-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Rename can_coerce
to may_coerce
, and then structurally resolve correctly in the probe
#131751
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1083,24 +1083,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
}) | ||
} | ||
|
||
/// Same as `coerce()`, but without side-effects. | ||
/// Probe whether `expr_ty` can be coerced to `target_ty`. This has no side-effects, | ||
/// and may return false positives if types are not yet fully constrained by inference. | ||
/// | ||
/// Returns false if the coercion creates any obligations that result in | ||
/// errors. | ||
pub(crate) fn can_coerce(&self, expr_ty: Ty<'tcx>, target: Ty<'tcx>) -> bool { | ||
// FIXME(-Znext-solver): We need to structurally resolve both types here. | ||
let source = self.resolve_vars_with_obligations(expr_ty); | ||
debug!("coercion::can_with_predicates({:?} -> {:?})", source, target); | ||
|
||
/// Returns false if the coercion is not possible, or if the coercion creates any | ||
/// sub-obligations that result in errors. | ||
/// | ||
/// This should only be used for diagnostics. | ||
pub(crate) fn may_coerce(&self, expr_ty: Ty<'tcx>, target_ty: Ty<'tcx>) -> bool { | ||
let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable); | ||
// We don't ever need two-phase here since we throw out the result of the coercion. | ||
// We also just always set `coerce_never` to true, since this is a heuristic. | ||
let coerce = Coerce::new(self, cause, AllowTwoPhase::No, true); | ||
let coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this hot enough that it may cause perf issues? Probably not, as this is only used in error paths There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yah, probably not. |
||
self.probe(|_| { | ||
let Ok(ok) = coerce.coerce(source, target) else { | ||
// Make sure to structurally resolve the types, since we use | ||
// the `TyKind`s heavily in coercion. | ||
let ocx = ObligationCtxt::new(self); | ||
let structurally_resolve = |ty| { | ||
let ty = self.shallow_resolve(ty); | ||
if self.next_trait_solver() | ||
&& let ty::Alias(..) = ty.kind() | ||
{ | ||
ocx.structurally_normalize(&cause, self.param_env, ty) | ||
} else { | ||
Ok(ty) | ||
} | ||
}; | ||
let Ok(expr_ty) = structurally_resolve(expr_ty) else { | ||
return false; | ||
}; | ||
let Ok(target_ty) = structurally_resolve(target_ty) else { | ||
return false; | ||
}; | ||
|
||
let Ok(ok) = coerce.coerce(expr_ty, target_ty) else { | ||
return false; | ||
}; | ||
let ocx = ObligationCtxt::new(self); | ||
ocx.register_obligations(ok.obligations); | ||
ocx.select_where_possible().is_empty() | ||
}) | ||
|
@@ -1369,7 +1387,7 @@ pub fn can_coerce<'tcx>( | |
) -> bool { | ||
let root_ctxt = crate::typeck_root_ctxt::TypeckRootCtxt::new(tcx, body_id); | ||
let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, body_id); | ||
fn_ctxt.can_coerce(ty, output_ty) | ||
fn_ctxt.may_coerce(ty, output_ty) | ||
} | ||
|
||
/// CoerceMany encapsulates the pattern you should use when you have | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/ui/traits/next-solver/diagnostics/coerce-in-may-coerce.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//@ compile-flags: -Znext-solver | ||
|
||
trait Mirror { | ||
type Assoc; | ||
} | ||
impl<T> Mirror for T { | ||
type Assoc = T; | ||
} | ||
|
||
fn arg() -> &'static [i32; 1] { todo!() } | ||
|
||
fn arg_error(x: <fn() as Mirror>::Assoc, y: ()) { todo!() } | ||
|
||
fn main() { | ||
// Should suggest to reverse the args... | ||
// but if we don't normalize the expected, then we don't. | ||
arg_error((), || ()); | ||
//~^ ERROR arguments to this function are incorrect | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.