-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Rustc confuses lifetimes of different unrelated variables of type &'static str when implementing a trait for &'a str #26448
Comments
…a.3. Tracking issue in rust for this issue: rust-lang/rust#26448
cc #29188 |
Just ran head-first into this. Here's a reduced example, tested on playpen nightly as of 2016-01-21 and 1.7.0-nightly commit 4405944 use std::marker::PhantomData;
struct Ident<Output>(PhantomData<Output>);
trait ScanFromStr {
fn scan_from() -> Option<&'static str>;
}
impl<'a, Output> ScanFromStr for Ident<Output>
where &'a str: Into<Output> {
fn scan_from() -> Option<&'static str> {
Some("expected identifier")
}
}
fn main() {} However, niconii on IRC discovered that this particular case can be worked around like so: use std::marker::PhantomData;
struct Ident<Output>(PhantomData<Output>);
trait ScanFromStr {
fn scan_from() -> Option<&'static str>;
}
impl<'a, Output> ScanFromStr for Ident<Output>
where for<'b> &'b str: Into<Output> {
fn scan_from() -> Option<&'static str> {
Some("expected identifier")
}
}
fn main() {} (Note the modification to the Edit: sadly, this doesn't help with the original, unreduced case, as the solution causes new problems ( |
Note that, due to rust-lang/rust#26448 some `ScanFromStr` implementations *cannot* return a `Syntax` error without trigger the aforementioned issue. This is what `SyntaxNoMessage` is for, until it gets fixed.
I've also run into something that is suspiciously similar, except that it fails to compile with a different error message on stable, but compiles correctly on beta. This seems really strange, and makes me wonder if maybe this is interacting badly with something else... use std::marker::PhantomData;
pub enum ScanErrorKind {
Syntax(&'static str),
}
pub trait ScanFromStr<'a>: Sized {
type Output;
fn scan_from(s: &'a str) -> Result<(Self::Output, usize), ScanErrorKind>;
}
pub struct Everything<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);
impl<'a, Output> ScanFromStr<'a> for Everything<'a, Output> where &'a str: Into<Output> {
type Output = Output;
fn scan_from(s: &'a str) -> Result<(Self::Output, usize), ScanErrorKind> {
Ok((s.into(), s.len()))
}
}
fn main() {} Compiling with stable .\local\boom.rs:13:23: 13:48 error: mismatched types:
expected `core::marker::Sized`,
found `core::marker::Sized`
(lifetime mismatch) [E0308]
.\local\boom.rs:13 fn scan_from() -> Result<(), ScanErrorKind> {
^~~~~~~~~~~~~~~~~~~~~~~~~
.\local\boom.rs:13:23: 13:48 help: run `rustc --explain E0308` to see a detailed explanation
.\local\boom.rs:13:49: 15:6 note: the lifetime 'a as defined on the block at 13:48...
.\local\boom.rs:13 fn scan_from() -> Result<(), ScanErrorKind> {
.\local\boom.rs:14 Ok(())
.\local\boom.rs:15 }
note: ...does not necessarily outlive the static lifetime
error: aborting due to previous error Current beta and nightly compile just fine. Interestingly, if I change |
* Added compatibility information to crate docs and readme. * Added an extra rule to `scan!` for trailing commas. * Modified syntax of an internal macro, because `where` isn't in `ty`'s follow set yet. * Had to work around what appears to be an even worse case of rust-lang/rust#26448. This involved dropping the generic impls of some scanners and supporting *only* `&str` and `String`.
Triage: all of OP's examples now compile on both editions. |
Tried to read through the issue when I got this notification and had no idea what it was, then looked at date! Can't believe it's been 3 years. Thank you for finding the issue and retesting it. |
The fix appears to have been in |
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#22892. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#28587. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes #10876. Closes #26448. Closes #26577. Closes #26619. Closes #27054. Closes #44127. Closes #44255. Closes #55731. Closes #57781.
…hton Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes rust-lang#10876. Closes rust-lang#26448. Closes rust-lang#26577. Closes rust-lang#26619. Closes rust-lang#27054. Closes rust-lang#44127. Closes rust-lang#44255. Closes rust-lang#55731. Closes rust-lang#57781.
Add tests for several E-needstest issues This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise. Closes #10876. Closes #26448. Closes #26577. Closes #26619. Closes #27054. Closes #44127. Closes #44255. Closes #55731. Closes #57781.
Example 1:
Results in:
Playground: https://play.rust-lang.org/?gist=22052e30a6d8e19c053e&version=stable.
Example 2:
Results in:
Playground: https://play.rust-lang.org/?gist=1bffa565c5fd39f942d1&version=nightly
Example 3:
Results in:
Playground: https://play.rust-lang.org/?gist=fd54c0069ab0f38079b7&version=stable
The first two examples occur on
rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
,rustc 1.1.0-beta.3 (6aa2d5078 2015-06-12)
andrustc 1.2.0-nightly (2f5683913 2015-06-18)
.The third example occurs only on
rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
andrustc 1.1.0-beta.3 (6aa2d5078 2015-06-12)
, and appears to be fixed in nightly. I've included it to contrast with the first and second examples, which are still broken in the nightly.The text was updated successfully, but these errors were encountered: