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

Normalize in infcx instead of globally for Option::as_deref suggestion #112303

Merged
merged 2 commits into from
Jun 6, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3592,8 +3592,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Extract `<U as Deref>::Target` assoc type and check that it is `T`
&& let Some(deref_target_did) = tcx.lang_items().deref_target()
&& let projection = tcx.mk_projection(deref_target_did, tcx.mk_substs(&[ty::GenericArg::from(found_ty)]))
&& let Ok(deref_target) = tcx.try_normalize_erasing_regions(param_env, projection)
&& deref_target == target_ty
&& let InferOk { value: deref_target, obligations } = infcx.at(&ObligationCause::dummy(), param_env).normalize(projection)
&& obligations.iter().all(|obligation| infcx.predicate_must_hold_modulo_regions(obligation))
&& infcx.can_eq(param_env, deref_target, target_ty)
{
let help = if let hir::Mutability::Mut = needs_mut
&& let Some(deref_mut_did) = tcx.lang_items().deref_mut_trait()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn deref_int(a: &i32) -> i32 {
*a
}

fn main() {
// https://github.com/rust-lang/rust/issues/112293
let _has_inference_vars: Option<i32> = Some(0).map(deref_int);
//~^ ERROR type mismatch in function arguments
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef-inference-var.rs:7:56
|
LL | fn deref_int(a: &i32) -> i32 {
| ---------------------------- found signature defined here
...
LL | let _has_inference_vars: Option<i32> = Some(0).map(deref_int);
| --- ^^^^^^^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected function signature `fn({integer}) -> _`
found function signature `for<'a> fn(&'a i32) -> _`
note: required by a bound in `Option::<T>::map`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: do not borrow the argument
|
LL - fn deref_int(a: &i32) -> i32 {
LL + fn deref_int(a: i32) -> i32 {
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0631`.
6 changes: 0 additions & 6 deletions tests/ui/mismatched_types/suggest-option-asderef-unfixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ fn no_args() -> Option<()> {
Some(())
}

fn generic_ref<T>(_: &T) -> Option<()> {
Some(())
}

extern "C" fn takes_str_but_wrong_abi(_: &str) -> Option<()> {
Some(())
}
Expand All @@ -33,8 +29,6 @@ fn main() {
//~^ ERROR expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}`
let _ = produces_string().and_then(no_args);
//~^ ERROR function is expected to take 1 argument, but it takes 0 arguments
let _ = produces_string().and_then(generic_ref);
//~^ ERROR type mismatch in function arguments
let _ = Some(TypeWithoutDeref).and_then(takes_str_but_too_many_refs);
//~^ ERROR type mismatch in function arguments
}
33 changes: 6 additions & 27 deletions tests/ui/mismatched_types/suggest-option-asderef-unfixable.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef-unfixable.rs:28:40
--> $DIR/suggest-option-asderef-unfixable.rs:24:40
|
LL | fn takes_str_but_too_many_refs(_: &&str) -> Option<()> {
| ------------------------------------------------------ found signature defined here
Expand All @@ -15,7 +15,7 @@ note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: expected a `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}`
--> $DIR/suggest-option-asderef-unfixable.rs:30:40
--> $DIR/suggest-option-asderef-unfixable.rs:26:40
|
LL | let _ = produces_string().and_then(takes_str_but_wrong_abi);
| -------- ^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}`
Expand All @@ -27,7 +27,7 @@ note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}`
--> $DIR/suggest-option-asderef-unfixable.rs:32:40
--> $DIR/suggest-option-asderef-unfixable.rs:28:40
|
LL | let _ = produces_string().and_then(takes_str_but_unsafe);
| -------- ^^^^^^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
Expand All @@ -40,7 +40,7 @@ note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
--> $DIR/suggest-option-asderef-unfixable.rs:34:40
--> $DIR/suggest-option-asderef-unfixable.rs:30:40
|
LL | fn no_args() -> Option<()> {
| -------------------------- takes 0 arguments
Expand All @@ -54,28 +54,7 @@ note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef-unfixable.rs:36:40
|
LL | fn generic_ref<T>(_: &T) -> Option<()> {
| -------------------------------------- found signature defined here
...
LL | let _ = produces_string().and_then(generic_ref);
| -------- ^^^^^^^^^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected function signature `fn(String) -> _`
found function signature `for<'a> fn(&'a _) -> _`
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: do not borrow the argument
|
LL - fn generic_ref<T>(_: &T) -> Option<()> {
LL + fn generic_ref<T>(_: T) -> Option<()> {
|

error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef-unfixable.rs:38:45
--> $DIR/suggest-option-asderef-unfixable.rs:32:45
|
LL | fn takes_str_but_too_many_refs(_: &&str) -> Option<()> {
| ------------------------------------------------------ found signature defined here
Expand All @@ -90,7 +69,7 @@ LL | let _ = Some(TypeWithoutDeref).and_then(takes_str_but_too_many_refs);
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL

error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0277, E0593, E0631.
For more information about an error, try `rustc --explain E0277`.
9 changes: 9 additions & 0 deletions tests/ui/mismatched_types/suggest-option-asderef.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ fn generic<T>(_: T) -> Option<()> {
Some(())
}

fn generic_ref<T>(_: T) -> Option<()> {
//~^ HELP do not borrow the argument
Some(())
}

fn main() {
let _: Option<()> = produces_string().as_deref().and_then(takes_str);
//~^ ERROR type mismatch in function arguments
Expand All @@ -27,4 +32,8 @@ fn main() {
//~^ ERROR type mismatch in function arguments
//~| HELP call `Option::as_deref_mut()` first
let _ = produces_string().and_then(generic);

let _ = produces_string().as_deref().and_then(generic_ref);
//~^ ERROR type mismatch in function arguments
//~| HELP call `Option::as_deref()` first
}
9 changes: 9 additions & 0 deletions tests/ui/mismatched_types/suggest-option-asderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ fn generic<T>(_: T) -> Option<()> {
Some(())
}

fn generic_ref<T>(_: &T) -> Option<()> {
//~^ HELP do not borrow the argument
Some(())
}

fn main() {
let _: Option<()> = produces_string().and_then(takes_str);
//~^ ERROR type mismatch in function arguments
Expand All @@ -27,4 +32,8 @@ fn main() {
//~^ ERROR type mismatch in function arguments
//~| HELP call `Option::as_deref_mut()` first
let _ = produces_string().and_then(generic);

let _ = produces_string().and_then(generic_ref);
//~^ ERROR type mismatch in function arguments
//~| HELP call `Option::as_deref()` first
}
33 changes: 29 additions & 4 deletions tests/ui/mismatched_types/suggest-option-asderef.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef.rs:20:52
--> $DIR/suggest-option-asderef.rs:25:52
|
LL | fn takes_str(_: &str) -> Option<()> {
| ----------------------------------- found signature defined here
Expand All @@ -19,7 +19,7 @@ LL | let _: Option<()> = produces_string().as_deref().and_then(takes_str);
| +++++++++++

error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef.rs:23:55
--> $DIR/suggest-option-asderef.rs:28:55
|
LL | fn takes_str(_: &str) -> Option<()> {
| ----------------------------------- found signature defined here
Expand All @@ -39,7 +39,7 @@ LL | let _: Option<Option<()>> = produces_string().as_deref().map(takes_str)
| +++++++++++

error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef.rs:26:55
--> $DIR/suggest-option-asderef.rs:31:55
|
LL | fn takes_str_mut(_: &mut str) -> Option<()> {
| ------------------------------------------- found signature defined here
Expand All @@ -58,6 +58,31 @@ help: call `Option::as_deref_mut()` first
LL | let _: Option<Option<()>> = produces_string().as_deref_mut().map(takes_str_mut);
| +++++++++++++++

error: aborting due to 3 previous errors
error[E0631]: type mismatch in function arguments
--> $DIR/suggest-option-asderef.rs:36:40
|
LL | fn generic_ref<T>(_: &T) -> Option<()> {
| -------------------------------------- found signature defined here
...
LL | let _ = produces_string().and_then(generic_ref);
| -------- ^^^^^^^^^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected function signature `fn(String) -> _`
found function signature `for<'a> fn(&'a _) -> _`
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: do not borrow the argument
|
LL - fn generic_ref<T>(_: &T) -> Option<()> {
LL + fn generic_ref<T>(_: T) -> Option<()> {
|
help: call `Option::as_deref()` first
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now shows up because of the first commit :)

|
LL | let _ = produces_string().as_deref().and_then(generic_ref);
| +++++++++++

error: aborting due to 4 previous errors

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