Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove a type string comparison #100349

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};

let self_ty = self.typeck_results.borrow().expr_ty(&method_expr[0]);
let self_ty = format!("{:?}", self_ty);
let name = method_path.ident.name;
let is_as_ref_able = (self_ty.starts_with("&std::option::Option")
|| self_ty.starts_with("&std::result::Result")
|| self_ty.starts_with("std::option::Option")
|| self_ty.starts_with("std::result::Result"))
&& (name == sym::map || name == sym::and_then);
let is_as_ref_able = match self_ty.peel_refs().kind() {
ty::Adt(def, _) => {
(self.tcx.is_diagnostic_item(sym::Option, def.did())
|| self.tcx.is_diagnostic_item(sym::Result, def.did()))
&& (name == sym::map || name == sym::and_then)
}
_ => false,
};
match (is_as_ref_able, self.sess().source_map().span_to_snippet(method_path.ident.span)) {
(true, Ok(src)) => {
let suggestion = format!("as_ref().{}", src);
Expand Down Expand Up @@ -792,7 +794,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ if is_range_literal(expr) => true,
_ => false,
};
let sugg_expr = if needs_parens { format!("({src})") } else { src };

if let Some(sugg) = self.can_use_as_ref(expr) {
return Some((
Expand Down Expand Up @@ -820,6 +821,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let sugg_expr = if needs_parens { format!("({src})") } else { src };
return Some(match mutability {
hir::Mutability::Mut => (
sp,
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/suggestions/as-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ fn main() {
// note: do not suggest because of `E: usize`
let x: &Result<usize, usize> = &Ok(3);
let y: Result<&usize, usize> = x; //~ ERROR mismatched types [E0308]

let multiple_ref_opt = &&Some(Foo);
multiple_ref_opt.map(|arg| takes_ref(arg)); //~ ERROR mismatched types [E0308]
multiple_ref_opt.and_then(|arg| Some(takes_ref(arg))); //~ ERROR mismatched types [E0308]
let multiple_ref_result = &&Ok(Foo);
multiple_ref_result.map(|arg| takes_ref(arg)); //~ ERROR mismatched types [E0308]
multiple_ref_result.and_then(|arg| Ok(takes_ref(arg))); //~ ERROR mismatched types [E0308]
}
62 changes: 61 additions & 1 deletion src/test/ui/suggestions/as-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,66 @@ LL | let y: Result<&usize, usize> = x;
= note: expected enum `Result<&usize, usize>`
found reference `&Result<usize, usize>`

error: aborting due to 7 previous errors
error[E0308]: mismatched types
--> $DIR/as-ref.rs:22:42
|
LL | multiple_ref_opt.map(|arg| takes_ref(arg));
| --- --------- ^^^ expected `&Foo`, found struct `Foo`
| | |
| | arguments to this function are incorrect
| help: consider using `as_ref` instead: `as_ref().map`
|
note: function defined here
--> $DIR/as-ref.rs:3:4
|
LL | fn takes_ref(_: &Foo) {}
| ^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/as-ref.rs:23:52
|
LL | multiple_ref_opt.and_then(|arg| Some(takes_ref(arg)));
| -------- --------- ^^^ expected `&Foo`, found struct `Foo`
| | |
| | arguments to this function are incorrect
| help: consider using `as_ref` instead: `as_ref().and_then`
|
note: function defined here
--> $DIR/as-ref.rs:3:4
|
LL | fn takes_ref(_: &Foo) {}
| ^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/as-ref.rs:25:45
|
LL | multiple_ref_result.map(|arg| takes_ref(arg));
| --- --------- ^^^ expected `&Foo`, found struct `Foo`
| | |
| | arguments to this function are incorrect
| help: consider using `as_ref` instead: `as_ref().map`
|
note: function defined here
--> $DIR/as-ref.rs:3:4
|
LL | fn takes_ref(_: &Foo) {}
| ^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/as-ref.rs:26:53
|
LL | multiple_ref_result.and_then(|arg| Ok(takes_ref(arg)));
| -------- --------- ^^^ expected `&Foo`, found struct `Foo`
| | |
| | arguments to this function are incorrect
| help: consider using `as_ref` instead: `as_ref().and_then`
|
note: function defined here
--> $DIR/as-ref.rs:3:4
|
LL | fn takes_ref(_: &Foo) {}
| ^^^^^^^^^ -------

error: aborting due to 11 previous errors

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