Skip to content

Taking an argument hides lifetime error #77912

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

Closed
camelid opened this issue Oct 13, 2020 · 16 comments
Closed

Taking an argument hides lifetime error #77912

camelid opened this issue Oct 13, 2020 · 16 comments
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@camelid
Copy link
Member

camelid commented Oct 13, 2020

This compiles without error:

fn foo(s: &i32) -> &i32 {
    &0
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: unused variable: `s`
 --> src/lib.rs:1:8
  |
1 | fn foo(s: &i32) -> &i32 {
  |        ^ help: if this is intentional, prefix it with an underscore: `_s`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: function is never used: `foo`
 --> src/lib.rs:1:4
  |
1 | fn foo(s: &i32) -> &i32 {
  |    ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 2 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.73s


But this errors, even though there shouldn't be a difference as s is not used in the previous example:

fn foo() -> &i32 {
    &0
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0106]: missing lifetime specifier
 --> src/lib.rs:1:13
  |
1 | fn foo() -> &i32 {
  |             ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
  |
1 | fn foo() -> &'static i32 {
  |             ^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0106`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.


Potentially related to #77910.

@camelid camelid added A-type-system Area: Type system A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Oct 13, 2020
@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

To be clear, I believe the bug occurs in the first example; I think the error in the second is correct.

@camelid camelid added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Oct 13, 2020
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 13, 2020
@jonas-schievink
Copy link
Contributor

When did this regress?

@jonas-schievink
Copy link
Contributor

Both cases look correct to me. The first one uses lifetime elision.

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Just finished bisecting :)

searched nightlies: from nightly-2019-01-01 to nightly-2020-10-13
regressed nightly: nightly-2020-05-03
searched commits: from 7f65393 to f05a524
regressed commit: dae90c1

bisected with cargo-bisect-rustc v0.5.2

Host triple: x86_64-apple-darwin
Reproduce with:

cargo bisect-rustc --preserve --regress=success --start=2019-01-01 

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Both cases look correct to me. The first one uses lifetime elision.

How so? They should be no different in the errors they produce. This is the diff between them:

-fn foo(s: &i32) -> &i32 {
+fn foo() -> &i32 {
    &0
}

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Also the bitcode commit :/

Cc @alexcrichton

@jonas-schievink
Copy link
Contributor

What was the behavior before that PR?

This documents the behavior: https://doc.rust-lang.org/reference/lifetime-elision.html?highlight=lifetime,elision#lifetime-elision-in-functions

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

I'm not sure; Godbolt is showing the same behavior throughout but bisect-rustc found a spot where it changed. Not sure what's going on...

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Oops, this is why:

$ cargo +bisector-nightly-2020-05-02-x86_64-apple-darwin rustc
   Compiling rust-issue-77912-a v0.1.0 (/Users/user/rust-issue-77912-a)
error: unknown codegen option: `embed-bitcode`

error: could not compile `rust-issue-77912-a`

To learn more, run the command again with --verbose.

@lcnr
Copy link
Contributor

lcnr commented Oct 13, 2020

I also think this behavior is expected, so I don't feel like there is a bug here

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Sorry @alexcrichton I think that was a false positive.

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Why would this be expected?

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Example A takes one argument (but does not use it) and does not error, while Example B takes no arguments and does error.

@jonas-schievink
Copy link
Contributor

Yeah, this is clearly working as specified in the reference (linked above), as well as here: https://rust-lang.github.io/rfcs/0141-lifetime-elision.html#examples

@lcnr
Copy link
Contributor

lcnr commented Oct 13, 2020

example a is desugared to

fn foo<'a>(s: &'a i32) -> &'a i32 {
    &0
}

while we currently do not apply any desugarings in the second example. We may want to change this behavior in the future, but as of now the compiler agrees with the language specification here

@camelid
Copy link
Member Author

camelid commented Oct 13, 2020

Huh, yes I find that behavior very confusing. I guess this can be closed then. Thank you for walking me through this! It was really hurting my brain :)

@camelid camelid closed this as completed Oct 13, 2020
@camelid camelid removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. labels Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system C-bug Category: This is a bug. 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