Skip to content

Commit 26513eb

Browse files
Enforce supertrait outlives obligations hold when confirming impl
1 parent cd90d5c commit 26513eb

7 files changed

+88
-11
lines changed

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

+30
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};
@@ -35,6 +36,7 @@ use rustc_hir::def_id::DefId;
3536
use rustc_infer::infer::BoundRegionConversionTime;
3637
use rustc_infer::infer::BoundRegionConversionTime::HigherRankedType;
3738
use rustc_infer::infer::DefineOpaqueTypes;
39+
use rustc_infer::traits::util::elaborate;
3840
use rustc_infer::traits::TraitObligation;
3941
use rustc_middle::dep_graph::dep_kinds;
4042
use rustc_middle::dep_graph::DepNodeIndex;
@@ -2795,6 +2797,34 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
27952797
});
27962798
}
27972799

2800+
if matches!(self.tcx().def_kind(def_id), DefKind::Impl { of_trait: true })
2801+
&& let Some(header) = self.tcx().impl_trait_header(def_id)
2802+
{
2803+
let trait_clause: ty::Clause<'tcx> =
2804+
header.trait_ref.instantiate(self.tcx(), args).to_predicate(self.tcx());
2805+
for clause in elaborate(self.tcx(), [trait_clause]) {
2806+
if matches!(
2807+
clause.kind().skip_binder(),
2808+
ty::ClauseKind::TypeOutlives(..) | ty::ClauseKind::RegionOutlives(..)
2809+
) {
2810+
let clause = normalize_with_depth_to(
2811+
self,
2812+
param_env,
2813+
cause.clone(),
2814+
recursion_depth,
2815+
clause,
2816+
&mut obligations,
2817+
);
2818+
obligations.push(Obligation {
2819+
cause: cause.clone(),
2820+
recursion_depth,
2821+
param_env,
2822+
predicate: clause.as_predicate(),
2823+
});
2824+
}
2825+
}
2826+
}
2827+
27982828
obligations
27992829
}
28002830
}

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

+1
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

+15-2
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

+1
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

+28-2
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`.
+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
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`.
7-
81
use std::fmt::Display;
92

103
trait Static: 'static {}
@@ -19,5 +12,6 @@ where
1912

2013
fn main() {
2114
let s = foo(&String::from("blah blah blah"));
15+
//~^ ERROR temporary value dropped while borrowed
2216
println!("{}", s);
2317
}
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:14: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`.

0 commit comments

Comments
 (0)