-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Deal with unnormalized projections when structurally resolving types with new solver #110204
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
bors
merged 2 commits into
rust-lang:master
from
compiler-errors:new-solver-hir-typeck-hacks
May 23, 2023
Merged
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
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
55 changes: 55 additions & 0 deletions
55
compiler/rustc_trait_selection/src/traits/structural_normalize.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,55 @@ | ||
use rustc_infer::infer::at::At; | ||
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; | ||
use rustc_infer::traits::{FulfillmentError, TraitEngine}; | ||
use rustc_middle::ty::{self, Ty}; | ||
|
||
use crate::traits::{query::evaluate_obligation::InferCtxtExt, NormalizeExt, Obligation}; | ||
|
||
pub trait StructurallyNormalizeExt<'tcx> { | ||
fn structurally_normalize( | ||
&self, | ||
ty: Ty<'tcx>, | ||
fulfill_cx: &mut dyn TraitEngine<'tcx>, | ||
) -> Result<Ty<'tcx>, Vec<FulfillmentError<'tcx>>>; | ||
} | ||
|
||
impl<'tcx> StructurallyNormalizeExt<'tcx> for At<'_, 'tcx> { | ||
fn structurally_normalize( | ||
&self, | ||
mut ty: Ty<'tcx>, | ||
fulfill_cx: &mut dyn TraitEngine<'tcx>, | ||
) -> Result<Ty<'tcx>, Vec<FulfillmentError<'tcx>>> { | ||
assert!(!ty.is_ty_var(), "should have resolved vars before calling"); | ||
compiler-errors marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if self.infcx.tcx.trait_solver_next() { | ||
while let ty::Alias(ty::Projection, projection_ty) = *ty.kind() { | ||
let new_infer_ty = self.infcx.next_ty_var(TypeVariableOrigin { | ||
kind: TypeVariableOriginKind::NormalizeProjectionType, | ||
span: self.cause.span, | ||
}); | ||
let obligation = Obligation::new( | ||
self.infcx.tcx, | ||
self.cause.clone(), | ||
self.param_env, | ||
ty::Binder::dummy(ty::ProjectionPredicate { | ||
projection_ty, | ||
term: new_infer_ty.into(), | ||
}), | ||
); | ||
if self.infcx.predicate_may_hold(&obligation) { | ||
fulfill_cx.register_predicate_obligation(self.infcx, obligation); | ||
let errors = fulfill_cx.select_where_possible(self.infcx); | ||
if !errors.is_empty() { | ||
return Err(errors); | ||
} | ||
ty = self.infcx.resolve_vars_if_possible(new_infer_ty); | ||
} else { | ||
break; | ||
} | ||
} | ||
Ok(ty) | ||
} else { | ||
Ok(self.normalize(ty).into_value_registering_obligations(self.infcx, fulfill_cx)) | ||
} | ||
} | ||
} |
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,25 @@ | ||
// compile-flags: -Ztrait-solver=next | ||
// check-pass | ||
|
||
// Verify that we can assemble inherent impl candidates on a possibly | ||
// unnormalized self type. | ||
|
||
trait Foo { | ||
type Assoc; | ||
} | ||
impl Foo for i32 { | ||
type Assoc = Bar; | ||
} | ||
|
||
struct Bar; | ||
impl Bar { | ||
fn method(&self) {} | ||
} | ||
|
||
fn build<T: Foo>(_: T) -> T::Assoc { | ||
todo!() | ||
} | ||
|
||
fn main() { | ||
build(1i32).method(); | ||
} |
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,13 @@ | ||
// compile-flags: -Ztrait-solver=next | ||
// check-pass | ||
|
||
#[derive(Default)] | ||
struct Foo { | ||
x: i32, | ||
} | ||
|
||
fn main() { | ||
let mut xs = <[Foo; 1]>::default(); | ||
xs[0].x = 1; | ||
(&mut xs[0]).x = 2; | ||
} |
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.
when do we actually hit that path? do we have an existing test for it?
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.
No :( doesn't seem like anything hits it.
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.
I personally would prefer to ICE here to get a test 😁 but ah well, r=me with or without
bug!
:p