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

use of undeclared lifetime emits incorrect help message #70152

Closed
lcnr opened this issue Mar 19, 2020 · 3 comments · Fixed by #82717
Closed

use of undeclared lifetime emits incorrect help message #70152

lcnr opened this issue Mar 19, 2020 · 3 comments · Fixed by #82717
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

@lcnr
Copy link
Contributor

lcnr commented Mar 19, 2020

In case there is an undeclared lifetime in an ADT deriving Eq a wrong help message is emited

#[derive(Eq, PartialEq)]
struct Test {
    a: &'b str,
}

causes the following error

error[E0261]: use of undeclared lifetime name `'b`
 --> src/lib.rs:3:9
  |
2 | struct Test {
  |            - help: consider introducing lifetime `'b` here: `<'b>`
3 |     a: &'b str,
  |         ^^ undeclared lifetime

error[E0261]: use of undeclared lifetime name `'b`
 --> src/lib.rs:3:9
  |
3 |     a: &'b str,
  |         ^^ undeclared lifetime
  |
help: consider introducing lifetime `'b` here
  |
1 | #[derive(<'b>, PartialEq)]
  |          ^^^^
help: consider introducing lifetime `'b` here
  |
1 | #[derive(<'b>, PartialEq)]
  |          ^^^^

thx, bye

@lcnr lcnr added the C-bug Category: This is a bug. label Mar 19, 2020
@rustbot rustbot removed the C-bug Category: This is a bug. label Mar 19, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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. labels Mar 19, 2020
@lcnr
Copy link
Contributor Author

lcnr commented Mar 19, 2020

@estebank estebank added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Mar 25, 2020
@estebank
Copy link
Contributor

estebank commented Mar 25, 2020

There are two things here:

  1. the suggestion should try and identify spans coming from macro expansion and not suggest changes
  2. #[derive(Eq)] in this case expands to the following:
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::cmp::Eq for Test {
    #[inline]
    #[doc(hidden)]
    fn assert_receiver_is_total_eq(&self) -> () {
        { let _: ::core::cmp::AssertParamIsEq<&'b str>; }
    }
}

Note the &'b str param, which could avoid having any lifetimes and thing would work correctly. We can probably change the desugaring to not specify the lifetime and then we would avoid the second unnecessary duplicated error.

@petrochenkov would you happen to know if there's any clear negative in doing 2 above and if not, point me in the right direction in librustc_expand to make that change?

@lcnr
Copy link
Contributor Author

lcnr commented May 2, 2020

I believe that we can't remove the explicit lifetime here.
This would allow Eq for &'a T even if Eq is only implemented for &'static T.

use std::marker::PhantomData;

trait A {}

struct Foo;

impl A for &'static Foo {}

struct AssertImpl<T: A>(PhantomData<T>);

pub fn f<'a>(_: &'a ()) {
    let _: AssertImpl::<&Foo>; 
}

JohnTitor added a commit to JohnTitor/rust that referenced this issue Mar 4, 2021
Account for macros when suggesting adding lifetime

Fix rust-lang#70152.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Mar 4, 2021
Account for macros when suggesting adding lifetime

Fix rust-lang#70152.
@bors bors closed this as completed in 5824917 Mar 4, 2021
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 A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

Successfully merging a pull request may close this issue.

4 participants