-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Do not call object_lifetime_default on lifetime params. #101214
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1148,21 +1148,18 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { | |
} | ||
} | ||
|
||
fn object_lifetime_default<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
param_def_id: DefId, | ||
) -> Option<ObjectLifetimeDefault> { | ||
fn object_lifetime_default<'tcx>(tcx: TyCtxt<'tcx>, param_def_id: DefId) -> ObjectLifetimeDefault { | ||
debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam); | ||
let param_def_id = param_def_id.expect_local(); | ||
let parent_def_id = tcx.local_parent(param_def_id); | ||
let generics = tcx.hir().get_generics(parent_def_id)?; | ||
let generics = tcx.hir().get_generics(parent_def_id).unwrap(); | ||
let param_hir_id = tcx.local_def_id_to_hir_id(param_def_id); | ||
let param = generics.params.iter().find(|p| p.hir_id == param_hir_id)?; | ||
let param = generics.params.iter().find(|p| p.hir_id == param_hir_id).unwrap(); | ||
|
||
// Scan the bounds and where-clauses on parameters to extract bounds | ||
// of the form `T:'a` so as to determine the `ObjectLifetimeDefault` | ||
// for each type parameter. | ||
match param.kind { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be |
||
GenericParamKind::Lifetime { .. } => None, | ||
GenericParamKind::Type { .. } => { | ||
let mut set = Set1::Empty; | ||
|
||
|
@@ -1181,21 +1178,17 @@ fn object_lifetime_default<'tcx>( | |
} | ||
} | ||
|
||
Some(match set { | ||
match set { | ||
Set1::Empty => ObjectLifetimeDefault::Empty, | ||
Set1::One(hir::LifetimeName::Static) => ObjectLifetimeDefault::Static, | ||
Set1::One(hir::LifetimeName::Param(param_def_id, _)) => { | ||
ObjectLifetimeDefault::Param(param_def_id.to_def_id()) | ||
} | ||
_ => ObjectLifetimeDefault::Ambiguous, | ||
}) | ||
} | ||
} | ||
GenericParamKind::Const { .. } => { | ||
// Generic consts don't impose any constraints. | ||
// | ||
// We still store a dummy value here to allow generic parameters | ||
// in an arbitrary order. | ||
Some(ObjectLifetimeDefault::Empty) | ||
_ => { | ||
bug!("object_lifetime_default_raw must only be called on a type parameter") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we include some extra info on what this method was called on? |
||
} | ||
} | ||
} | ||
|
@@ -1512,7 +1505,20 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { | |
generics | ||
.params | ||
.iter() | ||
.filter_map(|param| self.tcx.object_lifetime_default(param.def_id)) | ||
.filter_map(|param| { | ||
match self.tcx.def_kind(param.def_id) { | ||
// Generic consts don't impose any constraints. | ||
// | ||
// We still store a dummy value here to allow generic parameters | ||
// in an arbitrary order. | ||
DefKind::ConstParam => Some(ObjectLifetimeDefault::Empty), | ||
DefKind::TyParam => Some(self.tcx.object_lifetime_default(param.def_id)), | ||
// We may also get a `Trait` or `TraitAlias` because of how generics `Self` parameter | ||
// works. Ignore it because it can't have a meaningful lifetime default. | ||
DefKind::LifetimeParam | DefKind::Trait | DefKind::TraitAlias => None, | ||
dk => bug!("unexpected def_kind {:?}", dk), | ||
} | ||
}) | ||
.map(set_to_region) | ||
.collect() | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,50 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
#[rustc_object_lifetime_default] | ||
struct A<T>(T); //~ ERROR BaseDefault | ||
struct A< | ||
T, //~ ERROR BaseDefault | ||
>(T); | ||
|
||
#[rustc_object_lifetime_default] | ||
struct B<'a,T>(&'a (), T); //~ ERROR BaseDefault | ||
struct B< | ||
'a, | ||
T, //~ ERROR BaseDefault | ||
>(&'a (), T); | ||
|
||
#[rustc_object_lifetime_default] | ||
struct C<'a,T:'a>(&'a T); //~ ERROR 'a | ||
struct C< | ||
'a, | ||
T: 'a, //~ ERROR 'a | ||
>(&'a T); | ||
|
||
#[rustc_object_lifetime_default] | ||
struct D<'a,'b,T:'a+'b>(&'a T, &'b T); //~ ERROR Ambiguous | ||
struct D< | ||
'a, | ||
'b, | ||
T: 'a + 'b, //~ ERROR Ambiguous | ||
>(&'a T, &'b T); | ||
|
||
#[rustc_object_lifetime_default] | ||
struct E<'a,'b:'a,T:'b>(&'a T, &'b T); //~ ERROR 'b | ||
struct E< | ||
'a, | ||
'b: 'a, | ||
T: 'b, //~ ERROR 'b | ||
>(&'a T, &'b T); | ||
|
||
#[rustc_object_lifetime_default] | ||
struct F<'a,'b,T:'a,U:'b>(&'a T, &'b U); //~ ERROR 'a,'b | ||
struct F< | ||
'a, | ||
'b, | ||
T: 'a, //~ ERROR 'a | ||
U: 'b, //~ ERROR 'b | ||
>(&'a T, &'b U); | ||
|
||
#[rustc_object_lifetime_default] | ||
struct G<'a,'b,T:'a,U:'a+'b>(&'a T, &'b U); //~ ERROR 'a,Ambiguous | ||
|
||
fn main() { } | ||
struct G< | ||
'a, | ||
'b, | ||
T: 'a, //~ ERROR 'a | ||
U: 'a + 'b, //~ ERROR Ambiguous | ||
>(&'a T, &'b U); | ||
|
||
fn main() {} |
60 changes: 36 additions & 24 deletions
60
src/test/ui/object-lifetime/object-lifetime-default.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,56 @@ | ||
error: BaseDefault | ||
--> $DIR/object-lifetime-default.rs:4:1 | ||
--> $DIR/object-lifetime-default.rs:5:5 | ||
| | ||
LL | struct A<T>(T); | ||
| ^^^^^^^^^^^^^^^ | ||
LL | T, | ||
| ^ | ||
|
||
error: BaseDefault | ||
--> $DIR/object-lifetime-default.rs:7:1 | ||
--> $DIR/object-lifetime-default.rs:11:5 | ||
| | ||
LL | struct B<'a,T>(&'a (), T); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | T, | ||
| ^ | ||
|
||
error: 'a | ||
--> $DIR/object-lifetime-default.rs:10:1 | ||
--> $DIR/object-lifetime-default.rs:17:5 | ||
| | ||
LL | struct C<'a,T:'a>(&'a T); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | T: 'a, | ||
| ^ | ||
|
||
error: Ambiguous | ||
--> $DIR/object-lifetime-default.rs:13:1 | ||
--> $DIR/object-lifetime-default.rs:24:5 | ||
| | ||
LL | struct D<'a,'b,T:'a+'b>(&'a T, &'b T); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | T: 'a + 'b, | ||
| ^ | ||
|
||
error: 'b | ||
--> $DIR/object-lifetime-default.rs:16:1 | ||
--> $DIR/object-lifetime-default.rs:31:5 | ||
| | ||
LL | struct E<'a,'b:'a,T:'b>(&'a T, &'b T); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | T: 'b, | ||
| ^ | ||
|
||
error: 'a,'b | ||
--> $DIR/object-lifetime-default.rs:19:1 | ||
error: 'a | ||
--> $DIR/object-lifetime-default.rs:38:5 | ||
| | ||
LL | T: 'a, | ||
| ^ | ||
|
||
error: 'b | ||
--> $DIR/object-lifetime-default.rs:39:5 | ||
| | ||
LL | struct F<'a,'b,T:'a,U:'b>(&'a T, &'b U); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | U: 'b, | ||
| ^ | ||
|
||
error: 'a,Ambiguous | ||
--> $DIR/object-lifetime-default.rs:22:1 | ||
error: 'a | ||
--> $DIR/object-lifetime-default.rs:46:5 | ||
| | ||
LL | T: 'a, | ||
| ^ | ||
|
||
error: Ambiguous | ||
--> $DIR/object-lifetime-default.rs:47:5 | ||
| | ||
LL | struct G<'a,'b,T:'a,U:'a+'b>(&'a T, &'b U); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | U: 'a + 'b, | ||
| ^ | ||
|
||
error: aborting due to 7 previous errors | ||
error: aborting due to 9 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be replaced with
let
/else { return; }
, but isn't necessary in this PR.