Skip to content

Commit 44d1985

Browse files
Enforce supertrait outlives obligations hold when confirming impl
1 parent b01a977 commit 44d1985

File tree

10 files changed

+118
-10
lines changed

10 files changed

+118
-10
lines changed

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ where
8787
.map(|pred| goal.with(cx, pred));
8888
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
8989

90+
// We currently elaborate all supertrait obligations from impls. This
91+
// can be removed when we actually do coinduction correctly and just
92+
// register that the impl header must be WF.
93+
let goal_clause: I::Clause = goal.predicate.upcast(cx);
94+
for clause in elaborate::elaborate(cx, [goal_clause]) {
95+
if matches!(
96+
clause.kind().skip_binder(),
97+
ty::ClauseKind::TypeOutlives(..) | ty::ClauseKind::RegionOutlives(..)
98+
) {
99+
ecx.add_goal(GoalSource::Misc, goal.with(cx, clause));
100+
}
101+
}
102+
90103
ecx.evaluate_added_goals_and_make_canonical_response(maximal_certainty)
91104
})
92105
}

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::traits::project::ProjectAndUnifyResult;
2727
use crate::traits::project::ProjectionCacheKeyExt;
2828
use crate::traits::ProjectionCacheKey;
2929
use crate::traits::Unimplemented;
30+
use hir::def::DefKind;
3031
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
3132
use rustc_data_structures::stack::ensure_sufficient_stack;
3233
use rustc_errors::{Diag, EmissionGuarantee};
@@ -37,6 +38,7 @@ use rustc_infer::infer::relate::TypeRelation;
3738
use rustc_infer::infer::BoundRegionConversionTime;
3839
use rustc_infer::infer::BoundRegionConversionTime::HigherRankedType;
3940
use rustc_infer::infer::DefineOpaqueTypes;
41+
use rustc_infer::traits::util::elaborate;
4042
use rustc_infer::traits::TraitObligation;
4143
use rustc_middle::bug;
4244
use rustc_middle::dep_graph::dep_kinds;
@@ -2787,6 +2789,34 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
27872789
});
27882790
}
27892791

2792+
if matches!(self.tcx().def_kind(def_id), DefKind::Impl { of_trait: true })
2793+
&& let Some(header) = self.tcx().impl_trait_header(def_id)
2794+
{
2795+
let trait_clause: ty::Clause<'tcx> =
2796+
header.trait_ref.instantiate(self.tcx(), args).upcast(self.tcx());
2797+
for clause in elaborate(self.tcx(), [trait_clause]) {
2798+
if matches!(
2799+
clause.kind().skip_binder(),
2800+
ty::ClauseKind::TypeOutlives(..) | ty::ClauseKind::RegionOutlives(..)
2801+
) {
2802+
let clause = normalize_with_depth_to(
2803+
self,
2804+
param_env,
2805+
cause.clone(),
2806+
recursion_depth,
2807+
clause,
2808+
&mut obligations,
2809+
);
2810+
obligations.push(Obligation {
2811+
cause: cause.clone(),
2812+
recursion_depth,
2813+
param_env,
2814+
predicate: clause.as_predicate(),
2815+
});
2816+
}
2817+
}
2818+
}
2819+
27902820
obligations
27912821
}
27922822
}

compiler/rustc_type_ir/src/inherent.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ pub trait Clause<I: Interner<Clause = Self>>:
450450
+ UpcastFrom<I, ty::Binder<I, ty::ClauseKind<I>>>
451451
+ UpcastFrom<I, ty::TraitRef<I>>
452452
+ UpcastFrom<I, ty::Binder<I, ty::TraitRef<I>>>
453+
+ UpcastFrom<I, ty::TraitPredicate<I>>
454+
+ UpcastFrom<I, ty::Binder<I, ty::TraitPredicate<I>>>
453455
+ UpcastFrom<I, ty::ProjectionPredicate<I>>
454456
+ UpcastFrom<I, ty::Binder<I, ty::ProjectionPredicate<I>>>
455457
+ IntoKind<Kind = ty::Binder<I, ty::ClauseKind<I>>>

tests/ui/fn/implied-bounds-unnorm-associated-type-5.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ trait Trait<'a>: 'a {
55
// if the `T: 'a` bound gets implied we would probably get ub here again
66
impl<'a, T> Trait<'a> for T {
77
//~^ ERROR the parameter type `T` may not live long enough
8+
//~| ERROR the parameter type `T` may not live long enough
89
type Type = ();
910
}
1011

tests/ui/fn/implied-bounds-unnorm-associated-type-5.stderr

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,21 @@ help: consider adding an explicit lifetime bound
1616
LL | impl<'a, T: 'a> Trait<'a> for T {
1717
| ++++
1818

19+
error[E0309]: the parameter type `T` may not live long enough
20+
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:6:27
21+
|
22+
LL | impl<'a, T> Trait<'a> for T {
23+
| -- ^ ...so that the type `T` will meet its required lifetime bounds
24+
| |
25+
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
26+
|
27+
help: consider adding an explicit lifetime bound
28+
|
29+
LL | impl<'a, T: 'a> Trait<'a> for T {
30+
| ++++
31+
1932
error[E0505]: cannot move out of `x` because it is borrowed
20-
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:21:10
33+
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:22:10
2134
|
2235
LL | let x = String::from("Hello World!");
2336
| - binding `x` declared here
@@ -34,7 +47,7 @@ LL - let y = f(&x, ());
3447
LL + let y = f(x.clone(), ());
3548
|
3649

37-
error: aborting due to 2 previous errors
50+
error: aborting due to 3 previous errors
3851

3952
Some errors have detailed explanations: E0309, E0505.
4053
For more information about an error, try `rustc --explain E0309`.

tests/ui/static/static-lifetime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub trait Arbitrary: Sized + 'static {}
22

33
impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {} //~ ERROR lifetime bound
4+
//~^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a`
45

56
fn main() {
67
}

tests/ui/static/static-lifetime.stderr

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@ LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
1111
| ^^
1212
= note: but lifetime parameter must outlive the static lifetime
1313

14-
error: aborting due to 1 previous error
14+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
15+
--> $DIR/static-lifetime.rs:3:34
16+
|
17+
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
21+
--> $DIR/static-lifetime.rs:3:6
22+
|
23+
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
24+
| ^^
25+
note: ...so that the types are compatible
26+
--> $DIR/static-lifetime.rs:3:34
27+
|
28+
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
30+
= note: expected `<Cow<'a, A> as Arbitrary>`
31+
found `<Cow<'_, A> as Arbitrary>`
32+
= note: but, the lifetime must be valid for the static lifetime...
33+
note: ...so that the declared lifetime parameter bounds are satisfied
34+
--> $DIR/static-lifetime.rs:3:34
35+
|
36+
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error: aborting due to 2 previous errors
1540

16-
For more information about this error, try `rustc --explain E0478`.
41+
Some errors have detailed explanations: E0478, E0495.
42+
For more information about an error, try `rustc --explain E0478`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/wf-in-where-clause-static.rs:18:18
3+
|
4+
LL | let s = foo(&String::from("blah blah blah"));
5+
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
6+
| | |
7+
| | creates a temporary value which is freed while still in use
8+
| argument requires that borrow lasts for `'static`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0716`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/wf-in-where-clause-static.rs:18:18
3+
|
4+
LL | let s = foo(&String::from("blah blah blah"));
5+
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
6+
| | |
7+
| | creates a temporary value which is freed while still in use
8+
| argument requires that borrow lasts for `'static`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0716`.

tests/ui/wf/wf-in-where-clause-static.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
//@ check-pass
2-
//@ known-bug: #98117
3-
4-
// Should fail. Functions are responsible for checking the well-formedness of
5-
// their own where clauses, so this should fail and require an explicit bound
6-
// `T: 'static`.
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
74

85
use std::fmt::Display;
96

@@ -19,5 +16,6 @@ where
1916

2017
fn main() {
2118
let s = foo(&String::from("blah blah blah"));
19+
//~^ ERROR temporary value dropped while borrowed
2220
println!("{}", s);
2321
}

0 commit comments

Comments
 (0)