-
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
Fix unnamed lifetime spans #125234
Closed
Closed
Fix unnamed lifetime spans #125234
Changes from 2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
struct X; | ||
|
||
// Test that the error correctly differentiate between the two unnamed lifetimes | ||
impl std::ops::Add<&X> for &X { | ||
type Output = X; | ||
|
||
fn add(self, _rhs: Self) -> Self::Output { | ||
X | ||
} | ||
} | ||
//~^^^^ ERROR method not compatible with trait | ||
|
||
// Test that the error correctly differentiate between named and the unnamed lifetimes | ||
impl<'a> std::ops::Mul<&'a X> for &X { | ||
type Output = X; | ||
|
||
fn mul(self, _rhs: Self) -> Self::Output { | ||
X | ||
} | ||
} | ||
//~^^^^ ERROR method not compatible with trait | ||
|
||
// Test that the error correctly differentiate between named and the unnamed lifetimes | ||
impl<'a> std::ops::Sub<&X> for &'a X { | ||
type Output = X; | ||
|
||
fn sub(self, _rhs: Self) -> Self::Output { | ||
X | ||
} | ||
} | ||
//~^^^^ ERROR method not compatible with trait | ||
|
||
// This should pass since the lifetime subtyping will pass typecheck | ||
impl<'a, 'b> std::ops::Div<&'a X> for &'b X where 'a : 'b { | ||
type Output = X; | ||
|
||
fn div(self, _rhs: Self) -> Self::Output { | ||
X | ||
} | ||
} | ||
|
||
fn main() {} |
60 changes: 60 additions & 0 deletions
60
tests/ui/inference/unnamed-lifetime-span-mixup-125143.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
error[E0308]: method not compatible with trait | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:7:5 | ||
| | ||
LL | fn add(self, _rhs: Self) -> Self::Output { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ||
| | ||
= note: expected signature `fn(&X, &X) -> X` | ||
found signature `fn(&X, &X) -> X` | ||
note: the anonymous lifetime as defined here... | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:4:20 | ||
| | ||
LL | impl std::ops::Add<&X> for &X { | ||
| ^ | ||
note: ...does not necessarily outlive the anonymous lifetime as defined here | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:4:28 | ||
| | ||
LL | impl std::ops::Add<&X> for &X { | ||
| ^ | ||
|
||
error[E0308]: method not compatible with trait | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:17:5 | ||
| | ||
LL | fn mul(self, _rhs: Self) -> Self::Output { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ||
| | ||
= note: expected signature `fn(&X, &'a X) -> X` | ||
found signature `fn(&X, &X) -> X` | ||
note: the lifetime `'a` as defined here... | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:14:6 | ||
| | ||
LL | impl<'a> std::ops::Mul<&'a X> for &X { | ||
| ^^ | ||
note: ...does not necessarily outlive the anonymous lifetime as defined here | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:14:35 | ||
| | ||
LL | impl<'a> std::ops::Mul<&'a X> for &X { | ||
| ^ | ||
|
||
error[E0308]: method not compatible with trait | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:27:5 | ||
| | ||
LL | fn sub(self, _rhs: Self) -> Self::Output { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ||
| | ||
= note: expected signature `fn(&'a X, &X) -> X` | ||
found signature `fn(&'a X, &'a X) -> X` | ||
note: the anonymous lifetime as defined here... | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:24:24 | ||
| | ||
LL | impl<'a> std::ops::Sub<&X> for &'a X { | ||
| ^ | ||
note: ...does not necessarily outlive the lifetime `'a` as defined here | ||
--> $DIR/unnamed-lifetime-span-mixup-125143.rs:24:6 | ||
| | ||
LL | impl<'a> std::ops::Sub<&X> for &'a X { | ||
| ^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
Oops, something went wrong.
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 is definitely not correct if
DefId
comes from a different crate. Please make this take aLocalDefId
, or ideally, do the comparison withHirId
instead.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.
I was not able to find a way to convert a DefId to a HirId, so I used a LocalDefId instead. The place where I use it already has expect_local() on the binding scope, so presumably the lifetime should also be local. However, I tried to use the same logic for late bound regions, and that caused a panic in a test for trying to unpack a non-local DefId. Is there a way to get the HirId?