Skip to content
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

E0507 suggestion for borrowing mistake inside of a macro suggests a syntax error #107976

Open
asottile opened this issue Feb 13, 2023 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@asottile
Copy link

asottile commented Feb 13, 2023

Code

here's a minimal case -- a bit nonsensical but pulled out of a larger example

Cargo.toml

[package]
name = "wat"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wat-derive = { path = "wat-derive" }

src/main.rs

use wat_derive::Example;

trait Example {
    fn f(&self);
}

#[derive(Example)]
struct S {
    x: Option<String>
}

fn main() {
    let s = S { x: None };
    s.f();
}

wat-derive/Cargo.toml

[package]
name = "wat-derive"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
quote = "1.0.23"
syn = "1.0.107"

[lib]
proc-macro = true

wat-derive/src/lib.rs

#[proc_macro_derive(Example)]
pub fn m(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    quote::quote! {
        impl Example for S {
            fn f(&self) {
                // borrowing mistake here
                if let Some(v) = self.x {
                    dbg!(v);
                }
            }
        }
    }.into()
}

Current output

$ cargo build
   Compiling wat v0.1.0 (/home/asottile/workspace/pre-commit-rs/y/wat)
error[E0507]: cannot move out of `self.x` as enum variant `Some` which is behind a shared reference
 --> src/main.rs:7:10
  |
7 | #[derive(Example)]
  |          ^^^^^^^
  |          |
  |          data moved here
  |          move occurs because `v` has type `String`, which does not implement the `Copy` trait
  |
  = note: this error originates in the derive macro `Example` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider borrowing here
  |
7 | #[derive(&Example)]
  |          +

For more information about this error, try `rustc --explain E0507`.
error: could not compile `wat` due to previous error

Desired output

anything else tbh -- I don't really know the correct thing to do here -- it would be nice if it pointed at the borrow error inside the macro-generated code, but I don't think rustc currently displays the generated code anywhere

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@asottile asottile added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 13, 2023
@chenyukang
Copy link
Member

Similar issue #107113

@chenyukang chenyukang self-assigned this Feb 24, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 16, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? `@ghost`

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 17, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? ``@ghost``

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
@chenyukang chenyukang removed their assignment Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants