Skip to content

Commit 5861815

Browse files
authored
Rollup merge of rust-lang#114931 - Urgau:revert-114052, r=compiler-errors
Revert PR rust-lang#114052 to fix invalid suggestion This PR reverts rust-lang#114052 to fix the invalid suggestion produced by the PR. Unfortunately the invalid suggestion cannot be improved from the current position where it's emitted since we lack enough information (is an assignment?, left or right?, ...) to be able to fix it here. Furthermore the previous wasn't wrong, just suboptimal, contrary to the current one which is just wrong. Added a regression test and commented out some code instead of removing it so we can use it later. Reopens rust-lang#114050 Fixes rust-lang#114925
2 parents 7ea4de9 + 1c73248 commit 5861815

9 files changed

+73
-61
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5353
|| self.suggest_no_capture_closure(err, expected, expr_ty)
5454
|| self.suggest_boxing_when_appropriate(err, expr.span, expr.hir_id, expected, expr_ty)
5555
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
56-
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected, expected_ty_expr)
56+
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected)
5757
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
5858
|| self.suggest_into(err, expr, expr_ty, expected)
5959
|| self.suggest_floating_point_literal(err, expr, expected)

compiler/rustc_hir_typeck/src/errors.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl HelpUseLatestEdition {
253253
}
254254

255255
#[derive(Subdiagnostic)]
256-
pub enum OptionResultRefMismatch<'tcx> {
256+
pub enum OptionResultRefMismatch {
257257
#[suggestion(
258258
hir_typeck_option_result_copied,
259259
code = ".copied()",
@@ -276,19 +276,20 @@ pub enum OptionResultRefMismatch<'tcx> {
276276
span: Span,
277277
def_path: String,
278278
},
279-
#[suggestion(
280-
hir_typeck_option_result_asref,
281-
code = ".as_ref()",
282-
style = "verbose",
283-
applicability = "machine-applicable"
284-
)]
285-
AsRef {
286-
#[primary_span]
287-
span: Span,
288-
def_path: String,
289-
expected_ty: Ty<'tcx>,
290-
expr_ty: Ty<'tcx>,
291-
},
279+
// FIXME: #114050
280+
// #[suggestion(
281+
// hir_typeck_option_result_asref,
282+
// code = ".as_ref()",
283+
// style = "verbose",
284+
// applicability = "machine-applicable"
285+
// )]
286+
// AsRef {
287+
// #[primary_span]
288+
// span: Span,
289+
// def_path: String,
290+
// expected_ty: Ty<'tcx>,
291+
// expr_ty: Ty<'tcx>,
292+
// },
292293
}
293294

294295
#[derive(Diagnostic)]

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10971097
expr: &hir::Expr<'_>,
10981098
expr_ty: Ty<'tcx>,
10991099
expected_ty: Ty<'tcx>,
1100-
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
11011100
) -> bool {
11021101
let ty::Adt(adt_def, args) = expr_ty.kind() else {
11031102
return false;
@@ -1115,7 +1114,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11151114
{
11161115
let expr_inner_ty = args.type_at(0);
11171116
let expected_inner_ty = expected_args.type_at(0);
1118-
if let &ty::Ref(_, ty, mutability) = expr_inner_ty.kind()
1117+
if let &ty::Ref(_, ty, _mutability) = expr_inner_ty.kind()
11191118
&& self.can_eq(self.param_env, ty, expected_inner_ty)
11201119
{
11211120
let def_path = self.tcx.def_path_str(adt_def.did());
@@ -1124,14 +1123,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11241123
errors::OptionResultRefMismatch::Copied {
11251124
span, def_path
11261125
}
1127-
} else if let Some(expected_ty_expr) = expected_ty_expr
1128-
// FIXME: suggest changes to both expressions to convert both to
1129-
// Option/Result<&T>
1130-
&& mutability.is_not()
1131-
{
1132-
errors::OptionResultRefMismatch::AsRef {
1133-
span: expected_ty_expr.span.shrink_to_hi(), expected_ty, expr_ty, def_path
1134-
}
11351126
} else if let Some(clone_did) = self.tcx.lang_items().clone_trait()
11361127
&& rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions(
11371128
self,

tests/ui/associated-types/dont-suggest-cyclic-constraint.fixed

-13
This file was deleted.

tests/ui/associated-types/dont-suggest-cyclic-constraint.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-rustfix
2-
31
use std::fmt::Debug;
42

53
pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)

tests/ui/associated-types/dont-suggest-cyclic-constraint.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
error[E0308]: mismatched types
2-
--> $DIR/dont-suggest-cyclic-constraint.rs:9:35
2+
--> $DIR/dont-suggest-cyclic-constraint.rs:7:35
33
|
44
LL | debug_assert_eq!(iter.next(), Some(value));
55
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
66
|
77
= note: expected enum `Option<<I as Iterator>::Item>`
88
found enum `Option<&<I as Iterator>::Item>`
9-
help: use `Option::as_ref` to convert `Option<<I as Iterator>::Item>` to `Option<&<I as Iterator>::Item>`
10-
|
11-
LL | debug_assert_eq!(iter.next().as_ref(), Some(value));
12-
| +++++++++
139

1410
error: aborting due to previous error
1511

tests/ui/suggestions/copied-and-cloned.fixed

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
fn expect<T>(_: T) {}
44

5+
struct Issue114925 {
6+
x: Option<String>,
7+
}
8+
9+
fn issue_114925(lol: &mut Issue114925, x: Option<&String>) {
10+
lol.x = x.clone().cloned();
11+
//~^ ERROR mismatched types
12+
//~| HELP use `Option::cloned` to clone the value inside the `Option`
13+
}
14+
515
fn main() {
616
let x = Some(&());
717
expect::<Option<()>>(x.copied());
@@ -24,10 +34,10 @@ fn main() {
2434
let s = String::new();
2535
let x = Some(s.clone());
2636
let y = Some(&s);
27-
println!("{}", x.as_ref() == y);
37+
println!("{}", x == y.cloned());
2838
//~^ ERROR mismatched types
29-
//~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
30-
39+
//~| HELP use `Option::cloned` to clone the value inside the `Option`
40+
//FIXME(#114050) ~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
3141

3242
let mut s = ();
3343
let x = Some(s);
@@ -42,4 +52,6 @@ fn main() {
4252
println!("{}", x == y.cloned());
4353
//~^ ERROR mismatched types
4454
//~| HELP use `Option::cloned` to clone the value inside the `Option`
55+
56+
issue_114925(&mut Issue114925 { x: None }, None);
4557
}

tests/ui/suggestions/copied-and-cloned.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
fn expect<T>(_: T) {}
44

5+
struct Issue114925 {
6+
x: Option<String>,
7+
}
8+
9+
fn issue_114925(lol: &mut Issue114925, x: Option<&String>) {
10+
lol.x = x.clone();
11+
//~^ ERROR mismatched types
12+
//~| HELP use `Option::cloned` to clone the value inside the `Option`
13+
}
14+
515
fn main() {
616
let x = Some(&());
717
expect::<Option<()>>(x);
@@ -26,8 +36,8 @@ fn main() {
2636
let y = Some(&s);
2737
println!("{}", x == y);
2838
//~^ ERROR mismatched types
29-
//~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
30-
39+
//~| HELP use `Option::cloned` to clone the value inside the `Option`
40+
//FIXME(#114050) ~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
3141

3242
let mut s = ();
3343
let x = Some(s);
@@ -42,4 +52,6 @@ fn main() {
4252
println!("{}", x == y);
4353
//~^ ERROR mismatched types
4454
//~| HELP use `Option::cloned` to clone the value inside the `Option`
55+
56+
issue_114925(&mut Issue114925 { x: None }, None);
4557
}

tests/ui/suggestions/copied-and-cloned.stderr

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
error[E0308]: mismatched types
2-
--> $DIR/copied-and-cloned.rs:7:26
2+
--> $DIR/copied-and-cloned.rs:10:13
3+
|
4+
LL | lol.x = x.clone();
5+
| ----- ^^^^^^^^^ expected `Option<String>`, found `Option<&String>`
6+
| |
7+
| expected due to the type of this binding
8+
|
9+
= note: expected enum `Option<String>`
10+
found enum `Option<&String>`
11+
help: use `Option::cloned` to clone the value inside the `Option`
12+
|
13+
LL | lol.x = x.clone().cloned();
14+
| +++++++++
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/copied-and-cloned.rs:17:26
318
|
419
LL | expect::<Option<()>>(x);
520
| -------------------- ^ expected `Option<()>`, found `Option<&()>`
@@ -19,7 +34,7 @@ LL | expect::<Option<()>>(x.copied());
1934
| +++++++++
2035

2136
error[E0308]: mismatched types
22-
--> $DIR/copied-and-cloned.rs:11:30
37+
--> $DIR/copied-and-cloned.rs:21:30
2338
|
2439
LL | expect::<Result<(), ()>>(x);
2540
| ------------------------ ^ expected `Result<(), ()>`, found `Result<&(), _>`
@@ -39,7 +54,7 @@ LL | expect::<Result<(), ()>>(x.copied());
3954
| +++++++++
4055

4156
error[E0308]: mismatched types
42-
--> $DIR/copied-and-cloned.rs:16:30
57+
--> $DIR/copied-and-cloned.rs:26:30
4358
|
4459
LL | expect::<Option<String>>(x);
4560
| ------------------------ ^ expected `Option<String>`, found `Option<&String>`
@@ -59,7 +74,7 @@ LL | expect::<Option<String>>(x.cloned());
5974
| +++++++++
6075

6176
error[E0308]: mismatched types
62-
--> $DIR/copied-and-cloned.rs:20:34
77+
--> $DIR/copied-and-cloned.rs:30:34
6378
|
6479
LL | expect::<Result<String, ()>>(x);
6580
| ---------------------------- ^ expected `Result<String, ()>`, found `Result<&String, _>`
@@ -79,20 +94,20 @@ LL | expect::<Result<String, ()>>(x.cloned());
7994
| +++++++++
8095

8196
error[E0308]: mismatched types
82-
--> $DIR/copied-and-cloned.rs:27:25
97+
--> $DIR/copied-and-cloned.rs:37:25
8398
|
8499
LL | println!("{}", x == y);
85100
| ^ expected `Option<String>`, found `Option<&String>`
86101
|
87102
= note: expected enum `Option<String>`
88103
found enum `Option<&String>`
89-
help: use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
104+
help: use `Option::cloned` to clone the value inside the `Option`
90105
|
91-
LL | println!("{}", x.as_ref() == y);
92-
| +++++++++
106+
LL | println!("{}", x == y.cloned());
107+
| +++++++++
93108

94109
error[E0308]: mismatched types
95-
--> $DIR/copied-and-cloned.rs:35:25
110+
--> $DIR/copied-and-cloned.rs:45:25
96111
|
97112
LL | println!("{}", x == y);
98113
| ^ expected `Option<()>`, found `Option<&mut ()>`
@@ -105,7 +120,7 @@ LL | println!("{}", x == y.copied());
105120
| +++++++++
106121

107122
error[E0308]: mismatched types
108-
--> $DIR/copied-and-cloned.rs:42:25
123+
--> $DIR/copied-and-cloned.rs:52:25
109124
|
110125
LL | println!("{}", x == y);
111126
| ^ expected `Option<String>`, found `Option<&mut String>`
@@ -117,6 +132,6 @@ help: use `Option::cloned` to clone the value inside the `Option`
117132
LL | println!("{}", x == y.cloned());
118133
| +++++++++
119134

120-
error: aborting due to 7 previous errors
135+
error: aborting due to 8 previous errors
121136

122137
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)