-
Notifications
You must be signed in to change notification settings - Fork 13.4k
check universes when instantiating infer vars with placeholders #109813
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
Closed
Closed
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 |
---|---|---|
|
@@ -303,6 +303,7 @@ impl<'tcx> InferCtxt<'tcx> { | |
infcx: self, | ||
span, | ||
for_universe, | ||
root_ct: ct, | ||
target_vid, | ||
})?; | ||
|
||
|
@@ -659,6 +660,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { | |
return Ok(result); | ||
} | ||
debug!("generalize: t={:?}", t); | ||
let tcx = self.infcx.tcx; | ||
|
||
// Check to see whether the type we are generalizing references | ||
// any other type variable related to `vid` via | ||
|
@@ -707,7 +709,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { | |
.borrow_mut() | ||
.type_variables() | ||
.new_var(self.for_universe, origin); | ||
let u = self.tcx().mk_ty_var(new_var_id); | ||
let u = tcx.mk_ty_var(new_var_id); | ||
|
||
// Record that we replaced `vid` with `new_var_id` as part of a generalization | ||
// operation. This is needed to detect cyclic types. To see why, see the | ||
|
@@ -725,9 +727,25 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { | |
// relatable. | ||
Ok(t) | ||
} | ||
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => { | ||
ty::Placeholder(placeholder) => { | ||
if self.for_universe.can_name(placeholder.universe) { | ||
Ok(t) | ||
} else { | ||
Err(TypeError::UniverseMismatch { | ||
variable: tcx.mk_ty_var(self.for_vid_sub_root).into(), | ||
placeholder: t.into(), | ||
}) | ||
} | ||
} | ||
// Need to manually relate aliases as `super_relate_tys` is not simply structural. | ||
ty::Alias(kind, ty::AliasTy { def_id, substs, .. }) => { | ||
let s = self.relate(substs, substs)?; | ||
Ok(if s == substs { t } else { self.infcx.tcx.mk_opaque(def_id, s) }) | ||
Ok(if s == substs { | ||
t | ||
} else { | ||
let alias_ty = self.infcx.tcx.mk_alias_ty(def_id, s); | ||
self.infcx.tcx.mk_alias(kind, alias_ty) | ||
}) | ||
} | ||
_ => relate::super_relate_tys(self, t, t), | ||
}?; | ||
|
@@ -812,13 +830,19 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { | |
} | ||
} | ||
} | ||
ty::ConstKind::Placeholder(placeholder) => { | ||
if self.for_universe.can_name(placeholder.universe) { | ||
Ok(c) | ||
} else { | ||
Err(TypeError::UniverseMismatch { | ||
variable: self.tcx().mk_ty_var(self.for_vid_sub_root).into(), | ||
placeholder: c.into(), | ||
}) | ||
} | ||
} | ||
// Need to manually relate as `super_relate_consts` is not simply structural. | ||
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }) => { | ||
Comment on lines
+843
to
844
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. Same here, though this requires changing |
||
let substs = self.relate_with_variance( | ||
ty::Variance::Invariant, | ||
ty::VarianceDiagInfo::default(), | ||
substs, | ||
substs, | ||
)?; | ||
let substs = self.relate(substs, substs)?; | ||
Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty())) | ||
} | ||
_ => relate::super_relate_consts(self, c, c), | ||
|
@@ -886,6 +910,9 @@ struct ConstInferUnifier<'cx, 'tcx> { | |
|
||
for_universe: ty::UniverseIndex, | ||
|
||
// The const we're generalizing. Used for the cyclic const error. | ||
root_ct: ty::Const<'tcx>, | ||
|
||
/// The vid of the const variable that is in the process of being | ||
/// instantiated; if we find this within the const we are folding, | ||
/// that means we would have created a cyclic const. | ||
|
@@ -927,6 +954,20 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for ConstInferUnifier<'_, 'tcx> { | |
} | ||
} | ||
} | ||
ty::Placeholder(placeholder) => { | ||
if self.for_universe.can_name(placeholder.universe) { | ||
Ok(t) | ||
} else { | ||
Err(TypeError::UniverseMismatch { | ||
variable: self | ||
.infcx | ||
.tcx | ||
.mk_const(self.target_vid, self.root_ct.ty()) | ||
.into(), | ||
placeholder: t.into(), | ||
}) | ||
} | ||
} | ||
ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => Ok(t), | ||
_ => t.try_super_fold_with(self), | ||
} | ||
|
@@ -1001,11 +1042,25 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for ConstInferUnifier<'_, 'tcx> { | |
}, | ||
}, | ||
); | ||
Ok(self.interner().mk_const(new_var_id, c.ty())) | ||
Ok(self.interner().mk_const(new_var_id, self.root_ct.ty())) | ||
} | ||
} | ||
} | ||
} | ||
ty::ConstKind::Placeholder(placeholder) => { | ||
if self.for_universe.can_name(placeholder.universe) { | ||
Ok(c) | ||
} else { | ||
Err(TypeError::UniverseMismatch { | ||
variable: self | ||
.infcx | ||
.tcx | ||
.mk_const(self.target_vid, self.root_ct.ty()) | ||
.into(), | ||
placeholder: c.into(), | ||
}) | ||
} | ||
} | ||
_ => c.try_super_fold_with(self), | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// regression test for #109505 | ||
#![feature(non_lifetime_binders)] | ||
#![allow(incomplete_features)] | ||
|
||
trait Other<U: ?Sized> {} | ||
|
||
impl<U: ?Sized> Other<U> for U {} | ||
|
||
// - o: `for<T> T: Trait<?0>` | ||
// - c: `for<U> U: Trait<U>` | ||
// - U = ?1 | ||
// - T = ?1 | ||
// - ?0 = ?1 | ||
|
||
#[rustfmt::skip] | ||
fn foo<U: ?Sized>() | ||
where | ||
for<T> T: Other<U> {} | ||
|
||
fn bar() { | ||
foo::<_>(); //~ ERROR the trait bound `T: Other<_>` is not satisfied | ||
} | ||
|
||
fn main() {} |
18 changes: 18 additions & 0 deletions
18
tests/ui/traits/non_lifetime_binders/universe-error1.stderr
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,18 @@ | ||
error[E0277]: the trait bound `T: Other<_>` is not satisfied | ||
--> $DIR/universe-error1.rs:21:11 | ||
| | ||
LL | foo::<_>(); | ||
| ^ the trait `Other<_>` is not implemented for `T` | ||
| | ||
note: required by a bound in `foo` | ||
--> $DIR/universe-error1.rs:18:15 | ||
| | ||
LL | fn foo<U: ?Sized>() | ||
| --- required by a bound in this function | ||
LL | where | ||
LL | for<T> T: Other<U> {} | ||
| ^^^^^^^^ required by this bound in `foo` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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.