Skip to content

[DO NOT MERGE] ADT implied bounds: consider 'static + elaborate #119882

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
83 changes: 46 additions & 37 deletions compiler/rustc_hir_analysis/src/outlives/explicit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::{self, OutlivesPredicate, TyCtxt};
use rustc_trait_selection::traits::elaborate;

use super::utils::*;

Expand All @@ -18,46 +19,54 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> {
&mut self,
tcx: TyCtxt<'tcx>,
def_id: DefId,
) -> &ty::EarlyBinder<RequiredPredicates<'tcx>> {
self.map.entry(def_id).or_insert_with(|| {
let predicates = if def_id.is_local() {
tcx.explicit_predicates_of(def_id)
} else {
tcx.predicates_of(def_id)
};
let mut required_predicates = RequiredPredicates::default();

// process predicates and convert to `RequiredPredicates` entry, see below
for &(predicate, span) in predicates.predicates {
match predicate.kind().skip_binder() {
ty::ClauseKind::TypeOutlives(OutlivesPredicate(ty, reg)) => {
insert_outlives_predicate(
tcx,
ty.into(),
reg,
span,
&mut required_predicates,
)
}
) -> ty::EarlyBinder<&RequiredPredicates<'tcx>> {
self.map
.entry(def_id)
.or_insert_with(|| {
let predicates = if def_id.is_local() {
tcx.explicit_predicates_of(def_id)
} else {
tcx.predicates_of(def_id)
};

let mut required_predicates = RequiredPredicates::default();

// process predicates and convert to `RequiredPredicates` entry, see below
let predicates = predicates
.predicates
.into_iter()
.flat_map(|&(p, span)| elaborate(tcx, [p]).map(move |p| (p, span)));
for (predicate, span) in predicates {
match predicate.kind().skip_binder() {
ty::ClauseKind::TypeOutlives(OutlivesPredicate(ty, reg)) => {
insert_outlives_predicate(
tcx,
ty.into(),
reg,
span,
&mut required_predicates,
)
}

ty::ClauseKind::RegionOutlives(OutlivesPredicate(reg1, reg2)) => {
insert_outlives_predicate(
tcx,
reg1.into(),
reg2,
span,
&mut required_predicates,
)
ty::ClauseKind::RegionOutlives(OutlivesPredicate(reg1, reg2)) => {
insert_outlives_predicate(
tcx,
reg1.into(),
reg2,
span,
&mut required_predicates,
)
}
ty::ClauseKind::Trait(_)
| ty::ClauseKind::Projection(_)
| ty::ClauseKind::ConstArgHasType(_, _)
| ty::ClauseKind::WellFormed(_)
| ty::ClauseKind::ConstEvaluatable(_) => {}
}
ty::ClauseKind::Trait(_)
| ty::ClauseKind::Projection(_)
| ty::ClauseKind::ConstArgHasType(_, _)
| ty::ClauseKind::WellFormed(_)
| ty::ClauseKind::ConstEvaluatable(_) => {}
}
}

ty::EarlyBinder::bind(required_predicates)
})
ty::EarlyBinder::bind(required_predicates)
})
.as_ref()
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn check_explicit_predicates<'tcx>(
);
let explicit_predicates = explicit_map.explicit_predicates_of(tcx, def_id);

for (outlives_predicate, &span) in explicit_predicates.as_ref().skip_binder() {
for (outlives_predicate, &span) in explicit_predicates.skip_binder() {
debug!("outlives_predicate = {:?}", &outlives_predicate);

// Careful: If we are inferring the effects of a `dyn Trait<..>`
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_analysis/src/outlives/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub(crate) fn insert_outlives_predicate<'tcx>(
) {
// If the `'a` region is bound within the field type itself, we
// don't want to propagate this constraint to the header.
if !is_free_region(outlived_region) {
let valid_outlived_region = is_free_region(outlived_region) || outlived_region.is_static();
if !valid_outlived_region {
return;
}

Expand Down
5 changes: 0 additions & 5 deletions tests/ui/lifetimes/lifetime-doesnt-live-long-enough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ impl<'a, T: ListItem<'a>> Collection for List<'a, T> {
}
}

struct Foo<T> {
foo: &'static T
//~^ ERROR may not live long enough
}

trait X<K>: Sized {
fn foo<'a, L: X<&'a Nested<K>>>();
//~^ ERROR may not live long enough
Expand Down
29 changes: 7 additions & 22 deletions tests/ui/lifetimes/lifetime-doesnt-live-long-enough.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
error[E0310]: the parameter type `T` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:19:10
|
LL | foo: &'static T
| ^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound
|
LL | struct Foo<T: 'static> {
| +++++++++

error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:41:33
--> $DIR/lifetime-doesnt-live-long-enough.rs:36:33
|
LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
Expand All @@ -26,7 +12,7 @@ LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() where K: 'a {
| +++++++++++

error[E0309]: the parameter type `M` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
--> $DIR/lifetime-doesnt-live-long-enough.rs:39:36
|
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
Expand All @@ -39,7 +25,7 @@ LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b + 'a>() {
| ++++

error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:24:19
--> $DIR/lifetime-doesnt-live-long-enough.rs:19:19
|
LL | fn foo<'a, L: X<&'a Nested<K>>>();
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
Expand All @@ -52,7 +38,7 @@ LL | fn foo<'a, L: X<&'a Nested<K>>>() where K: 'a;
| +++++++++++

error[E0309]: the parameter type `Self` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:28:19
--> $DIR/lifetime-doesnt-live-long-enough.rs:23:19
|
LL | fn bar<'a, L: X<&'a Nested<Self>>>();
| -- ^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<Self>` does not outlive the data it points at
Expand All @@ -65,7 +51,7 @@ LL | fn bar<'a, L: X<&'a Nested<Self>>>() where Self: 'a;
| ++++++++++++++

error[E0309]: the parameter type `L` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:32:22
--> $DIR/lifetime-doesnt-live-long-enough.rs:27:22
|
LL | fn baz<'a, L, M: X<&'a Nested<L>>>() {
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
Expand All @@ -77,7 +63,6 @@ help: consider adding an explicit lifetime bound
LL | fn baz<'a, L: 'a, M: X<&'a Nested<L>>>() {
| ++++

error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0309, E0310.
For more information about an error, try `rustc --explain E0309`.
For more information about this error, try `rustc --explain E0309`.
8 changes: 4 additions & 4 deletions tests/ui/rfcs/rfc-2093-infer-outlives/dont-infer-static.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* We don't infer `T: 'static` outlives relationships.
*/
// We don't infer `T: 'static` outlives relationships.

// check-pass

struct Foo<U> {
bar: Bar<U> //~ ERROR the parameter type `U` may not live long enough [E0310]
bar: Bar<U>,
}
struct Bar<T: 'static> {
x: T,
Expand Down
22 changes: 0 additions & 22 deletions tests/ui/rfcs/rfc-2093-infer-outlives/dont-infer-static.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Check that we rely on super trait bounds when computing implied bounds for ADTs.
//
// check-pass
struct Foo<U> {
bar: Bar<U>,
}

trait Trait: 'static {}
impl<T: 'static> Trait for T {}
struct Bar<T: Trait> {
x: T,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Check that we enforce WF conditions also for types in fns.

//
// check-pass

#![allow(dead_code)]

Expand All @@ -10,8 +11,7 @@ struct MustBeCopy<T:Copy> {
}

struct Foo<T> {
// needs T: 'static
x: dyn Object<&'static T> //~ ERROR E0310
x: dyn Object<&'static T>
}


Expand Down
6 changes: 6 additions & 0 deletions tests/ui/rfcs/rfc-2093-infer-outlives/unexpected-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
struct Foo<T> { t: &'static T }

fn main() {
let x = 3;
let _ = Foo { t: &x }; //~ ERROR `x` does not live long enough
}
16 changes: 16 additions & 0 deletions tests/ui/rfcs/rfc-2093-infer-outlives/unexpected-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0597]: `x` does not live long enough
--> $DIR/unexpected-error.rs:5:22
|
LL | let x = 3;
| - binding `x` declared here
LL | let _ = Foo { t: &x };
| ^^
| |
| borrowed value does not live long enough
| this usage requires that `x` is borrowed for `'static`
LL | }
| - `x` dropped here while still borrowed

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0597`.
15 changes: 6 additions & 9 deletions tests/ui/wf/wf-in-fn-type-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@

#![allow(dead_code)]


struct MustBeCopy<T:Copy> {
t: T
}

struct Foo<T> {
// needs T: 'static
x: fn() -> &'static T //~ ERROR E0310
x: fn() -> &'static T,
}

struct Bar<T> {
// needs T: Copy
x: fn(&'static T) //~ ERROR E0310
x: fn(&'static T),
}

fn not_static<T>() {
let _: Foo<T>; //~ ERROR E0310
let _: Bar<T>; //~ ERROR E0310
}

fn main() { }
32 changes: 16 additions & 16 deletions tests/ui/wf/wf-in-fn-type-static.stderr
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
error[E0310]: the parameter type `T` may not live long enough
--> $DIR/wf-in-fn-type-static.rs:13:8
--> $DIR/wf-in-fn-type-static.rs:15:12
|
LL | x: fn() -> &'static T
| ^^^^^^^^^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
LL | let _: Foo<T>;
| ^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
LL | struct Foo<T: 'static> {
| +++++++++
LL | fn not_static<T: 'static>() {
| +++++++++

error[E0310]: the parameter type `T` may not live long enough
--> $DIR/wf-in-fn-type-static.rs:18:8
--> $DIR/wf-in-fn-type-static.rs:16:12
|
LL | x: fn(&'static T)
| ^^^^^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
LL | let _: Bar<T>;
| ^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
LL | struct Bar<T: 'static> {
| +++++++++
LL | fn not_static<T: 'static>() {
| +++++++++

error: aborting due to 2 previous errors

Expand Down
17 changes: 0 additions & 17 deletions tests/ui/wf/wf-in-obj-type-static.stderr

This file was deleted.