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

E0726 makes invalid mid-keyword suggestion with proc-macro derived on struct #108256

Open
Zannick opened this issue Feb 20, 2023 · 3 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Zannick
Copy link

Zannick commented Feb 20, 2023

I tried this code:

extern crate sort_by_derive;

use sort_by_derive::SortBy;
use std::borrow::Cow;

pub trait Ctx: Clone {}

#[derive(Clone, SortBy)]
pub struct ContextWrapper<'a, T>
where T: Ctx,
{
    ctx: Cow<'a, T>,
    #[sort_by]
    elapsed: i32,
}

impl<'a, T: Ctx> ContextWrapper<'a, T> {
    pub fn new(ctx: T) -> ContextWrapper<'a, T> {
        ContextWrapper {
            ctx: Cow::Owned(ctx),
            elapsed: 0,
        }
    }
}

and in Cargo.toml:

[dependencies]
sort_by_derive = "0.1.10"

I expected either:

  1. the code compiles with cargo check
  2. any errors point out what needs to be changed

Instead, this error appeared:

error[E0726]: implicit elided lifetime not allowed here
  --> src/lib.rs:12:1
   |
12 | pub struct ContextWrapper<'a, T>
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ expected lifetime parameter
   |
   = note: assuming a `'static` lifetime...
help: indicate the anonymous lifetime
   |
12 | p'_, ub struct ContextWrapper<'a, T>
   |  +++

For more information about this error, try `rustc --explain E0726`.

I'm guessing the error being pointed to is in the procedural macro generated code, but that's not immediately clear as it points to my struct definition. This code compiles when I remove SortBy and its attribute.

Meta

rustc --version --verbose:

rustc 1.67.1 (d5a82bbd2 2023-02-07) (Fedora 1.67.1-1.fc37)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.7

(Issue template requested a backtrace but RUST_BACKTRACE=1 cargo build did not produce additional output.)

@Zannick Zannick added the C-bug Category: This is a bug. label Feb 20, 2023
@Zannick Zannick changed the title E0726 makes invalid mid-keyword suggestion with proc-macro-derived on struct E0726 makes invalid mid-keyword suggestion with proc-macro derived on struct Feb 20, 2023
@Noratrieb Noratrieb added A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 23, 2023
@estebank
Copy link
Contributor

estebank commented Mar 14, 2023

sort_by_derive Is effectively using the wrong span on their expanded code. If they were doing nothing then #109082 would eliminate the issue, but the best bet we have here is to modify sort_by_derive itself, instead of adding more heuristics to rustc to detect that the macros of the Token Stream coming out of a proc-macro might be misleading (at least in the short term).

@Zannick
Copy link
Author

Zannick commented Mar 14, 2023 via email

@valsteen
Copy link

hi, crate author here 👋

Indeed the problem is on my side and lifetimes were missing from the expanded code, so I fixed that and released the fix. I should probably check if I can highlight the derive macro itself when it generated something invalid, but it's going to be of limited value to the users, except indeed signaling that it's not their fault.

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 C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants