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

fix: skip implied bounds if unconstrained lifetime exists #110272

Merged
merged 3 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion compiler/rustc_trait_selection/src/traits/outlives_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
) -> Vec<OutlivesBound<'tcx>> {
let ty = self.resolve_vars_if_possible(ty);
let ty = OpportunisticRegionResolver::new(self).fold_ty(ty);
assert!(!ty.needs_infer());

// We must avoid processing unconstrained lifetime variables in implied
// bounds. See #110161 for context.
assert!(!ty.has_non_region_infer());
if ty.needs_infer() {
Copy link
Member

Choose a reason for hiding this comment

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

I would keep the assertion around for has_non_lifetime_infer and only delay a span bug if we have infer lifetimes.

Copy link
Member

Choose a reason for hiding this comment

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

I guess type vars has the same problem. For a test case, we can probably replace &'lt () by (X,) in the ui test. I'm not sure though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fwiw (and I'm completely inexperienced), I think we are good here, there aren't unconstrained type variables in the same way?

aliemjay marked this conversation as resolved.
Show resolved Hide resolved
self.tcx.sess.delay_span_bug(
self.tcx.def_span(body_id),
"skipped implied_outlives_bounds due to unconstrained lifetimes",
);
return vec![];
}

let span = self.tcx.def_span(body_id);
let result = param_env
Expand Down
26 changes: 26 additions & 0 deletions tests/ui/implied-bounds/issue-110161.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ICE regression relating to unconstrained lifetimes in implied
// bounds. See #110161.

// compile-flags: --crate-type=lib

trait LtTrait {
type Ty;
}

// erroneous `Ty` impl
impl LtTrait for () {
//~^ ERROR not all trait items implemented, missing: `Ty` [E0046]
}

// `'lt` is not constrained by the erroneous `Ty`
impl<'lt, T> LtTrait for Box<T>
where
T: LtTrait<Ty = &'lt ()>,
{
type Ty = &'lt ();
}

// unconstrained lifetime appears in implied bounds
fn test(_: <Box<()> as LtTrait>::Ty) {}

fn test2<'x>(_: &'x <Box<()> as LtTrait>::Ty) {}
12 changes: 12 additions & 0 deletions tests/ui/implied-bounds/issue-110161.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0046]: not all trait items implemented, missing: `Ty`
--> $DIR/issue-110161.rs:11:1
|
LL | type Ty;
| ------- `Ty` from trait
...
LL | impl LtTrait for () {
| ^^^^^^^^^^^^^^^^^^^ missing `Ty` in implementation

error: aborting due to previous error

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