-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Shallowly bail from coerce_unsized
more
#142941
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
Open
compiler-errors
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
compiler-errors:shallow-bail
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+41
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -515,11 +515,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { | |
/// | ||
/// [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions) | ||
#[instrument(skip(self), level = "debug")] | ||
fn coerce_unsized(&self, mut source: Ty<'tcx>, mut target: Ty<'tcx>) -> CoerceResult<'tcx> { | ||
source = self.shallow_resolve(source); | ||
target = self.shallow_resolve(target); | ||
debug!(?source, ?target); | ||
|
||
fn coerce_unsized(&self, source: Ty<'tcx>, target: Ty<'tcx>) -> CoerceResult<'tcx> { | ||
// We don't apply any coercions incase either the source or target | ||
// aren't sufficiently well known but tend to instead just equate | ||
// them both. | ||
|
@@ -532,6 +528,46 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { | |
return Err(TypeError::Mismatch); | ||
} | ||
|
||
// These targets are known to never be RHS in `LHS: CoerceUnsized<RHS>`. | ||
// That's because these are built-in types for which a core-provided impl | ||
// doesn't exist, and for which a user-written impl is invalid. | ||
// | ||
// This is technically incomplete when users write impossible bounds like | ||
// `where T: CoerceUnsized<usize>`, for example, but that trait is unstable | ||
// and coercion is allowed to be incomplete. The only case where this matters | ||
// is impossible bounds. | ||
match target.kind() { | ||
ty::Bool | ||
| ty::Char | ||
| ty::Int(_) | ||
| ty::Uint(_) | ||
| ty::Float(_) | ||
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) | ||
| ty::Str | ||
| ty::Array(_, _) | ||
| ty::Slice(_) | ||
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. array to slice is always 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. Slices are the RHS for Unsize, not CoerceUnsized. The latter is for pointers not data. |
||
| ty::FnDef(_, _) | ||
| ty::FnPtr(_, _) | ||
| ty::Dynamic(_, _, _) | ||
| ty::Closure(_, _) | ||
| ty::CoroutineClosure(_, _) | ||
| ty::Coroutine(_, _) | ||
| ty::CoroutineWitness(_, _) | ||
| ty::Never | ||
| ty::Tuple(_) => return Err(TypeError::Mismatch), | ||
_ => {} | ||
} | ||
// Additionally, we ignore `&str -> &str` coercions, which happen very | ||
// commonly since strings are one of the most used argument types in Rust, | ||
// we do coercions when type checking call expressions. | ||
if let ty::Ref(_, source_pointee, ty::Mutability::Not) = *source.kind() | ||
&& source_pointee.is_str() | ||
&& let ty::Ref(_, target_pointee, ty::Mutability::Not) = *target.kind() | ||
&& target_pointee.is_str() | ||
{ | ||
return Err(TypeError::Mismatch); | ||
} | ||
|
||
let traits = | ||
(self.tcx.lang_items().unsize_trait(), self.tcx.lang_items().coerce_unsized_trait()); | ||
let (Some(unsize_did), Some(coerce_unsized_did)) = traits else { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove these, are we guaranteed that they are shallow resolved? if so, please convert them into debug asserts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we shallowly resolve at the top of the coerce entrypoint