-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Suggest adding semicolon in user code rather than macro impl details #142543
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
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 hidden or 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 hidden or 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,52 @@ | ||
// Make sure the generated suggestion suggest editing the user | ||
// code instead of the std macro implementation | ||
|
||
//@ run-rustfix | ||
|
||
#![allow(dead_code)] | ||
|
||
use std::fmt::{self, Display}; | ||
|
||
struct Mutex; | ||
|
||
impl Mutex { | ||
fn lock(&self) -> MutexGuard<'_> { | ||
MutexGuard(self) | ||
} | ||
} | ||
|
||
struct MutexGuard<'a>(&'a Mutex); | ||
|
||
impl<'a> Drop for MutexGuard<'a> { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
struct Out; | ||
|
||
impl Out { | ||
fn write_fmt(&self, _args: fmt::Arguments) {} | ||
} | ||
|
||
impl<'a> Display for MutexGuard<'a> { | ||
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() { | ||
let _write = { | ||
let mutex = Mutex; | ||
write!(Out, "{}", mutex.lock()); | ||
//~^ ERROR `mutex` does not live long enough | ||
//~| SUGGESTION ; | ||
}; | ||
|
||
let _write = { | ||
use std::io::Write as _; | ||
|
||
let mutex = Mutex; | ||
let x = write!(std::io::stdout(), "{}", mutex.lock()); x | ||
//~^ ERROR `mutex` does not live long enough | ||
//~| SUGGESTION let x | ||
}; | ||
} |
This file contains hidden or 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,52 @@ | ||
// Make sure the generated suggestion suggest editing the user | ||
// code instead of the std macro implementation | ||
|
||
//@ run-rustfix | ||
|
||
#![allow(dead_code)] | ||
|
||
use std::fmt::{self, Display}; | ||
|
||
struct Mutex; | ||
|
||
impl Mutex { | ||
fn lock(&self) -> MutexGuard<'_> { | ||
MutexGuard(self) | ||
} | ||
} | ||
|
||
struct MutexGuard<'a>(&'a Mutex); | ||
|
||
impl<'a> Drop for MutexGuard<'a> { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
struct Out; | ||
|
||
impl Out { | ||
fn write_fmt(&self, _args: fmt::Arguments) {} | ||
} | ||
|
||
impl<'a> Display for MutexGuard<'a> { | ||
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() { | ||
let _write = { | ||
let mutex = Mutex; | ||
write!(Out, "{}", mutex.lock()) | ||
//~^ ERROR `mutex` does not live long enough | ||
//~| SUGGESTION ; | ||
}; | ||
|
||
let _write = { | ||
use std::io::Write as _; | ||
|
||
let mutex = Mutex; | ||
write!(std::io::stdout(), "{}", mutex.lock()) | ||
//~^ ERROR `mutex` does not live long enough | ||
//~| SUGGESTION let x | ||
}; | ||
} |
This file contains hidden or 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,47 @@ | ||
error[E0597]: `mutex` does not live long enough | ||
--> $DIR/span-semicolon-issue-139049.rs:39:27 | ||
| | ||
LL | let mutex = Mutex; | ||
| ----- binding `mutex` declared here | ||
LL | write!(Out, "{}", mutex.lock()) | ||
| ^^^^^------- | ||
| | | ||
| borrowed value does not live long enough | ||
| a temporary with access to the borrow is created here ... | ||
... | ||
LL | }; | ||
| -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard` | ||
| | | ||
| `mutex` dropped here while still borrowed | ||
| | ||
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped | ||
| | ||
LL | write!(Out, "{}", mutex.lock()); | ||
| + | ||
|
||
error[E0597]: `mutex` does not live long enough | ||
--> $DIR/span-semicolon-issue-139049.rs:48:41 | ||
| | ||
LL | let mutex = Mutex; | ||
| ----- binding `mutex` declared here | ||
LL | write!(std::io::stdout(), "{}", mutex.lock()) | ||
| ^^^^^------- | ||
| | | ||
| borrowed value does not live long enough | ||
| a temporary with access to the borrow is created here ... | ||
... | ||
LL | }; | ||
| -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard` | ||
| | | ||
| `mutex` dropped here while still borrowed | ||
| | ||
= note: the temporary is part of an expression at the end of a block; | ||
consider forcing this temporary to be dropped sooner, before the block's local variables are dropped | ||
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block | ||
| | ||
LL | let x = write!(std::io::stdout(), "{}", mutex.lock()); x | ||
| +++++++ +++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0597`. |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@Urgau #140748 makes this just work, so this new test blocks me from merging that PR. Should I remove it? Is there a different way to test what you want to test?No wait, the error still appears. Only the suggestion doesn't appear. Investigating..
Uh oh!
There was an error while loading. Please reload this page.
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 just noticed that this test passes in Rust 2024, due to the new rules for temporaries in tail expressions.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sure, but we still want to provide non-butchered / good diagnostics in older editions, they have "long-term support".
Uh oh!
There was an error while loading. Please reload this page.
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.
We could replace that test with the following one:
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.
Or modify
tests/ui/nll/issue-54556-used-vs-unused-tails.rs
and add it thereThere 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.
@fmease That test doesn't work. Then it just suggests to add the
;
inside the macro definition.