Skip to content

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 1 commit into from
Jun 16, 2025
Merged
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
10 changes: 7 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,18 @@ impl<'tcx> BorrowExplanation<'tcx> {
}
}
} else if let LocalInfo::BlockTailTemp(info) = local_decl.local_info() {
let sp = info
.span
.find_ancestor_in_same_ctxt(local_decl.source_info.span)
.unwrap_or(info.span);
if info.tail_result_is_ignored {
// #85581: If the first mutable borrow's scope contains
// the second borrow, this suggestion isn't helpful.
if !multiple_borrow_span.is_some_and(|(old, new)| {
old.to(info.span.shrink_to_hi()).contains(new)
}) {
err.span_suggestion_verbose(
info.span.shrink_to_hi(),
sp.shrink_to_hi(),
"consider adding semicolon after the expression so its \
temporaries are dropped sooner, before the local variables \
declared by the block are dropped",
Expand All @@ -368,8 +372,8 @@ impl<'tcx> BorrowExplanation<'tcx> {
local variable `x` and then make `x` be the expression at the \
end of the block",
vec![
(info.span.shrink_to_lo(), "let x = ".to_string()),
(info.span.shrink_to_hi(), "; x".to_string()),
(sp.shrink_to_lo(), "let x = ".to_string()),
(sp.shrink_to_hi(), "; x".to_string()),
],
Applicability::MaybeIncorrect,
);
Expand Down
52 changes: 52 additions & 0 deletions tests/ui/borrowck/span-semicolon-issue-139049.fixed
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
};
}
52 changes: 52 additions & 0 deletions tests/ui/borrowck/span-semicolon-issue-139049.rs
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 ;
};
Comment on lines +37 to +42
Copy link
Member

@m-ou-se m-ou-se Jun 16, 2025

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..

Copy link
Member

@m-ou-se m-ou-se Jun 16, 2025

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.

Copy link
Member

@fmease fmease Jun 16, 2025

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

Sure, but we still want to provide non-butchered / good diagnostics in older editions, they have "long-term support".

Copy link
Member

@fmease fmease Jun 16, 2025

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:

// Make sure the generated suggestion suggest editing the user code instead of
// the macro implementation (which might come from an external crate).
// issue: <https://github.com/rust-lang/rust/issues/139049>

// You could assume that this comes from an extern crate (it doesn't
// because an aux crate would be overkill for this test).
macro_rules! perform { ($e:expr) => { D(&$e).end() } }

fn main() {
    { let l = (); perform!(l) }; //~ ERROR does not live long enough
    //~^ SUGGESTION ;
    let _x = { let l = (); perform!(l) }; //~ ERROR does not live long enough
    //~| SUGGESTION let x
}

struct D<T>(T);
impl<T> Drop for D<T> { fn drop(&mut self) {} }
impl<T> D<T> { fn end(&self) -> String { String::new()} }

Copy link
Member

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 there

Copy link
Member

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.


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
};
}
47 changes: 47 additions & 0 deletions tests/ui/borrowck/span-semicolon-issue-139049.stderr
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`.
8 changes: 8 additions & 0 deletions tests/ui/macros/format-args-temporaries-in-write.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ LL | };
| |
| `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()); /* no semicolon */
| +

error[E0597]: `mutex` does not live long enough
--> $DIR/format-args-temporaries-in-write.rs:47:29
Expand All @@ -31,6 +35,10 @@ LL | };
| |
| `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 | writeln!(Out, "{}", mutex.lock()); /* no semicolon */
| +

error: aborting due to 2 previous errors

Expand Down
Loading