Skip to content

Commit

Permalink
Auto merge of rust-lang#124336 - compiler-errors:super-outlives, r=<try>
Browse files Browse the repository at this point in the history
[crater] Enforce supertrait outlives obligations hold when confirming impl

r? `@lcnr`

Fixes rust-lang#98117
  • Loading branch information
bors committed Apr 24, 2024
2 parents 5557f8c + 26513eb commit 311f8b6
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
30 changes: 30 additions & 0 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Expand Up @@ -27,6 +27,7 @@ use crate::traits::project::ProjectAndUnifyResult;
use crate::traits::project::ProjectionCacheKeyExt;
use crate::traits::ProjectionCacheKey;
use crate::traits::Unimplemented;
use hir::def::DefKind;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{Diag, EmissionGuarantee};
Expand All @@ -35,6 +36,7 @@ use rustc_hir::def_id::DefId;
use rustc_infer::infer::BoundRegionConversionTime;
use rustc_infer::infer::BoundRegionConversionTime::HigherRankedType;
use rustc_infer::infer::DefineOpaqueTypes;
use rustc_infer::traits::util::elaborate;
use rustc_infer::traits::TraitObligation;
use rustc_middle::dep_graph::dep_kinds;
use rustc_middle::dep_graph::DepNodeIndex;
Expand Down Expand Up @@ -2795,6 +2797,34 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
});
}

if matches!(self.tcx().def_kind(def_id), DefKind::Impl { of_trait: true })
&& let Some(header) = self.tcx().impl_trait_header(def_id)
{
let trait_clause: ty::Clause<'tcx> =
header.trait_ref.instantiate(self.tcx(), args).to_predicate(self.tcx());
for clause in elaborate(self.tcx(), [trait_clause]) {
if matches!(
clause.kind().skip_binder(),
ty::ClauseKind::TypeOutlives(..) | ty::ClauseKind::RegionOutlives(..)
) {
let clause = normalize_with_depth_to(
self,
param_env,
cause.clone(),
recursion_depth,
clause,
&mut obligations,
);
obligations.push(Obligation {
cause: cause.clone(),
recursion_depth,
param_env,
predicate: clause.as_predicate(),
});
}
}
}

obligations
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/fn/implied-bounds-unnorm-associated-type-5.rs
Expand Up @@ -5,6 +5,7 @@ trait Trait<'a>: 'a {
// if the `T: 'a` bound gets implied we would probably get ub here again
impl<'a, T> Trait<'a> for T {
//~^ ERROR the parameter type `T` may not live long enough
//~| ERROR the parameter type `T` may not live long enough
type Type = ();
}

Expand Down
17 changes: 15 additions & 2 deletions tests/ui/fn/implied-bounds-unnorm-associated-type-5.stderr
Expand Up @@ -16,8 +16,21 @@ help: consider adding an explicit lifetime bound
LL | impl<'a, T: 'a> Trait<'a> for T {
| ++++

error[E0309]: the parameter type `T` may not live long enough
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:6:27
|
LL | impl<'a, T> Trait<'a> for T {
| -- ^ ...so that the type `T` will meet its required lifetime bounds
| |
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound
|
LL | impl<'a, T: 'a> Trait<'a> for T {
| ++++

error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:21:10
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:22:10
|
LL | let x = String::from("Hello World!");
| - binding `x` declared here
Expand All @@ -34,7 +47,7 @@ LL - let y = f(&x, ());
LL + let y = f(x.clone(), ());
|

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0309, E0505.
For more information about an error, try `rustc --explain E0309`.
1 change: 1 addition & 0 deletions tests/ui/static/static-lifetime.rs
@@ -1,6 +1,7 @@
pub trait Arbitrary: Sized + 'static {}

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

fn main() {
}
30 changes: 28 additions & 2 deletions tests/ui/static/static-lifetime.stderr
Expand Up @@ -11,6 +11,32 @@ LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
| ^^
= note: but lifetime parameter must outlive the static lifetime

error: aborting due to 1 previous error
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> $DIR/static-lifetime.rs:3:34
|
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
--> $DIR/static-lifetime.rs:3:6
|
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
| ^^
note: ...so that the types are compatible
--> $DIR/static-lifetime.rs:3:34
|
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `<Cow<'a, A> as Arbitrary>`
found `<Cow<'_, A> as Arbitrary>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the declared lifetime parameter bounds are satisfied
--> $DIR/static-lifetime.rs:3:34
|
LL | impl<'a, A: Clone> Arbitrary for ::std::borrow::Cow<'a, A> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0478`.
Some errors have detailed explanations: E0478, E0495.
For more information about an error, try `rustc --explain E0478`.
8 changes: 1 addition & 7 deletions tests/ui/wf/wf-in-where-clause-static.rs
@@ -1,10 +1,3 @@
//@ check-pass
//@ known-bug: #98117

// Should fail. Functions are responsible for checking the well-formedness of
// their own where clauses, so this should fail and require an explicit bound
// `T: 'static`.

use std::fmt::Display;

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

fn main() {
let s = foo(&String::from("blah blah blah"));
//~^ ERROR temporary value dropped while borrowed
println!("{}", s);
}
12 changes: 12 additions & 0 deletions tests/ui/wf/wf-in-where-clause-static.stderr
@@ -0,0 +1,12 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/wf-in-where-clause-static.rs:14:18
|
LL | let s = foo(&String::from("blah blah blah"));
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`

error: aborting due to 1 previous error

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

0 comments on commit 311f8b6

Please sign in to comment.