Skip to content

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

Description

@Zannick

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions