Skip to content

Commit

Permalink
Rollup merge of rust-lang#114931 - Urgau:revert-114052, r=compiler-er…
Browse files Browse the repository at this point in the history
…rors

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
  • Loading branch information
cuviper authored Aug 17, 2023
2 parents 7ea4de9 + 1c73248 commit 5861815
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 61 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|| self.suggest_no_capture_closure(err, expected, expr_ty)
|| self.suggest_boxing_when_appropriate(err, expr.span, expr.hir_id, expected, expr_ty)
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected, expected_ty_expr)
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected)
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
|| self.suggest_into(err, expr, expr_ty, expected)
|| self.suggest_floating_point_literal(err, expr, expected)
Expand Down
29 changes: 15 additions & 14 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl HelpUseLatestEdition {
}

#[derive(Subdiagnostic)]
pub enum OptionResultRefMismatch<'tcx> {
pub enum OptionResultRefMismatch {
#[suggestion(
hir_typeck_option_result_copied,
code = ".copied()",
Expand All @@ -276,19 +276,20 @@ pub enum OptionResultRefMismatch<'tcx> {
span: Span,
def_path: String,
},
#[suggestion(
hir_typeck_option_result_asref,
code = ".as_ref()",
style = "verbose",
applicability = "machine-applicable"
)]
AsRef {
#[primary_span]
span: Span,
def_path: String,
expected_ty: Ty<'tcx>,
expr_ty: Ty<'tcx>,
},
// FIXME: #114050
// #[suggestion(
// hir_typeck_option_result_asref,
// code = ".as_ref()",
// style = "verbose",
// applicability = "machine-applicable"
// )]
// AsRef {
// #[primary_span]
// span: Span,
// def_path: String,
// expected_ty: Ty<'tcx>,
// expr_ty: Ty<'tcx>,
// },
}

#[derive(Diagnostic)]
Expand Down
11 changes: 1 addition & 10 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr: &hir::Expr<'_>,
expr_ty: Ty<'tcx>,
expected_ty: Ty<'tcx>,
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
) -> bool {
let ty::Adt(adt_def, args) = expr_ty.kind() else {
return false;
Expand All @@ -1115,7 +1114,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
let expr_inner_ty = args.type_at(0);
let expected_inner_ty = expected_args.type_at(0);
if let &ty::Ref(_, ty, mutability) = expr_inner_ty.kind()
if let &ty::Ref(_, ty, _mutability) = expr_inner_ty.kind()
&& self.can_eq(self.param_env, ty, expected_inner_ty)
{
let def_path = self.tcx.def_path_str(adt_def.did());
Expand All @@ -1124,14 +1123,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
errors::OptionResultRefMismatch::Copied {
span, def_path
}
} else if let Some(expected_ty_expr) = expected_ty_expr
// FIXME: suggest changes to both expressions to convert both to
// Option/Result<&T>
&& mutability.is_not()
{
errors::OptionResultRefMismatch::AsRef {
span: expected_ty_expr.span.shrink_to_hi(), expected_ty, expr_ty, def_path
}
} else if let Some(clone_did) = self.tcx.lang_items().clone_trait()
&& rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions(
self,
Expand Down
13 changes: 0 additions & 13 deletions tests/ui/associated-types/dont-suggest-cyclic-constraint.fixed

This file was deleted.

2 changes: 0 additions & 2 deletions tests/ui/associated-types/dont-suggest-cyclic-constraint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// run-rustfix

use std::fmt::Debug;

pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
error[E0308]: mismatched types
--> $DIR/dont-suggest-cyclic-constraint.rs:9:35
--> $DIR/dont-suggest-cyclic-constraint.rs:7:35
|
LL | debug_assert_eq!(iter.next(), Some(value));
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
|
= note: expected enum `Option<<I as Iterator>::Item>`
found enum `Option<&<I as Iterator>::Item>`
help: use `Option::as_ref` to convert `Option<<I as Iterator>::Item>` to `Option<&<I as Iterator>::Item>`
|
LL | debug_assert_eq!(iter.next().as_ref(), Some(value));
| +++++++++

error: aborting due to previous error

Expand Down
18 changes: 15 additions & 3 deletions tests/ui/suggestions/copied-and-cloned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

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

struct Issue114925 {
x: Option<String>,
}

fn issue_114925(lol: &mut Issue114925, x: Option<&String>) {
lol.x = x.clone().cloned();
//~^ ERROR mismatched types
//~| HELP use `Option::cloned` to clone the value inside the `Option`
}

fn main() {
let x = Some(&());
expect::<Option<()>>(x.copied());
Expand All @@ -24,10 +34,10 @@ fn main() {
let s = String::new();
let x = Some(s.clone());
let y = Some(&s);
println!("{}", x.as_ref() == y);
println!("{}", x == y.cloned());
//~^ ERROR mismatched types
//~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`

//~| HELP use `Option::cloned` to clone the value inside the `Option`
//FIXME(#114050) ~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`

let mut s = ();
let x = Some(s);
Expand All @@ -42,4 +52,6 @@ fn main() {
println!("{}", x == y.cloned());
//~^ ERROR mismatched types
//~| HELP use `Option::cloned` to clone the value inside the `Option`

issue_114925(&mut Issue114925 { x: None }, None);
}
16 changes: 14 additions & 2 deletions tests/ui/suggestions/copied-and-cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

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

struct Issue114925 {
x: Option<String>,
}

fn issue_114925(lol: &mut Issue114925, x: Option<&String>) {
lol.x = x.clone();
//~^ ERROR mismatched types
//~| HELP use `Option::cloned` to clone the value inside the `Option`
}

fn main() {
let x = Some(&());
expect::<Option<()>>(x);
Expand All @@ -26,8 +36,8 @@ fn main() {
let y = Some(&s);
println!("{}", x == y);
//~^ ERROR mismatched types
//~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`

//~| HELP use `Option::cloned` to clone the value inside the `Option`
//FIXME(#114050) ~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`

let mut s = ();
let x = Some(s);
Expand All @@ -42,4 +52,6 @@ fn main() {
println!("{}", x == y);
//~^ ERROR mismatched types
//~| HELP use `Option::cloned` to clone the value inside the `Option`

issue_114925(&mut Issue114925 { x: None }, None);
}
37 changes: 26 additions & 11 deletions tests/ui/suggestions/copied-and-cloned.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
error[E0308]: mismatched types
--> $DIR/copied-and-cloned.rs:7:26
--> $DIR/copied-and-cloned.rs:10:13
|
LL | lol.x = x.clone();
| ----- ^^^^^^^^^ expected `Option<String>`, found `Option<&String>`
| |
| expected due to the type of this binding
|
= note: expected enum `Option<String>`
found enum `Option<&String>`
help: use `Option::cloned` to clone the value inside the `Option`
|
LL | lol.x = x.clone().cloned();
| +++++++++

error[E0308]: mismatched types
--> $DIR/copied-and-cloned.rs:17:26
|
LL | expect::<Option<()>>(x);
| -------------------- ^ expected `Option<()>`, found `Option<&()>`
Expand All @@ -19,7 +34,7 @@ LL | expect::<Option<()>>(x.copied());
| +++++++++

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

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

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

error[E0308]: mismatched types
--> $DIR/copied-and-cloned.rs:27:25
--> $DIR/copied-and-cloned.rs:37:25
|
LL | println!("{}", x == y);
| ^ expected `Option<String>`, found `Option<&String>`
|
= note: expected enum `Option<String>`
found enum `Option<&String>`
help: use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
help: use `Option::cloned` to clone the value inside the `Option`
|
LL | println!("{}", x.as_ref() == y);
| +++++++++
LL | println!("{}", x == y.cloned());
| +++++++++

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

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

error: aborting due to 7 previous errors
error: aborting due to 8 previous errors

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

0 comments on commit 5861815

Please sign in to comment.