-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Fix lifetime on LocalInternedString::get function #59227
Conversation
panic!("`{:?}` is not a valid identifier", string) | ||
} | ||
if is_raw { | ||
let normalized_sym = Symbol::intern(string); | ||
let normalized_sym = sym.interned(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-interning here was intentional (to get rid of possible gensyms before checking).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence the interned
call.
@@ -2160,9 +2160,11 @@ impl<'a> Parser<'a> { | |||
suffix, | |||
) = self.token { | |||
let suffix = suffix.and_then(|s| { | |||
let s = s.as_str().get(); | |||
if ["f32", "f64"].contains(&s) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, shouldn't this keep working with non-static lifetime?
Also, a typo "f64f64" below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the LocalInternedString
is dropped in the first statement here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this is going to ruin the fn ident_str
API in #58899 then.
Treating an attribute name as &str
(including matching on it, calling contains
) is a common operation, so returning Option<LocalInternedString>
instead and map
ping on it every time would be a serious usability hit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. fn ident_str
won't work. We should really use symbols for attribute names instead of comparing strings all the time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, the issue is just that a lot of existing code across rustc uses strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in #59256
@@ -515,7 +515,7 @@ impl LocalInternedString { | |||
} | |||
} | |||
|
|||
pub fn get(&self) -> &'static str { | |||
pub fn get(&self) -> &str { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment and maybe an ui-fulldeps
test, to ensure this doesn't happen again?
And maybe self.string
should be *const str
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this use unsafe code. That should prevent people from modifying this without reading comments.
☔ The latest upstream changes (presumably #58899) made this pull request unmergeable. Please resolve the merge conflicts. |
r? @eddyb |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Thanks for fixing this, @Zoxc. It's certainly an interesting lesson for me in the potential non-local effects of |
src/libsyntax_pos/symbol.rs
Outdated
} | ||
} | ||
|
||
impl<T: std::ops::Deref<Target = str>> std::cmp::PartialEq<T> for LocalInternedString { | ||
fn eq(&self, other: &T) -> bool { | ||
self.string == other.deref() | ||
self.get() == other.deref() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO one should write *other
rather than .deref()
.
src/libsyntax_pos/symbol.rs
Outdated
@@ -530,37 +536,37 @@ where | |||
str: std::convert::AsRef<U> | |||
{ | |||
fn as_ref(&self) -> &U { | |||
self.string.as_ref() | |||
self.get().as_ref() | |||
} | |||
} | |||
|
|||
impl<T: std::ops::Deref<Target = str>> std::cmp::PartialEq<T> for LocalInternedString { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, isn't AsRef<str>
common practice in these sort of situations?
src/libsyntax_pos/symbol.rs
Outdated
} | ||
} | ||
|
||
impl std::cmp::PartialEq<LocalInternedString> for String { | ||
fn eq(&self, other: &LocalInternedString) -> bool { | ||
self == other.string | ||
self == other.get() | ||
} | ||
} | ||
|
||
impl<'a> std::cmp::PartialEq<LocalInternedString> for &'a String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are too many of these, maybe a macro should be used (which can do *other == *self
, I think).
r=me with nits fixed (if at all, they're kind of inconsequential) |
src/libsyntax_pos/symbol.rs
Outdated
@@ -510,18 +510,24 @@ fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T { | |||
// by creating a new thread right after constructing the interner. | |||
#[derive(Clone, Copy, Hash, PartialOrd, Eq, Ord)] | |||
pub struct LocalInternedString { | |||
string: &'static str, | |||
string: *const str, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized this should probably be NonNull<str>
.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Ping from triage, @Zoxc |
src/libsyntax_pos/symbol.rs
Outdated
@@ -514,18 +515,24 @@ fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T { | |||
// by creating a new thread right after constructing the interner. | |||
#[derive(Clone, Copy, Hash, PartialOrd, Eq, Ord)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argh, all the derives are now wrong...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just changed this back to &'static str
.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors r+ |
📌 Commit 5ea959d has been approved by |
🌲 The tree is currently closed for pull requests below priority 15, this pull request will be tested once the tree is reopened |
Fix lifetime on LocalInternedString::get function cc @eddyb @nnethercote
Fix lifetime on LocalInternedString::get function cc @eddyb @nnethercote
☀️ Test successful - checks-travis, status-appveyor |
📣 Toolstate changed by #59227! Tested on commit cd8b437. 💔 clippy-driver on windows: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk @phansch, @rust-lang/infra). |
Tested on commit rust-lang/rust@cd8b437. Direct link to PR: <rust-lang/rust#59227> 💔 clippy-driver on windows: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk @phansch, @rust-lang/infra). 💔 clippy-driver on linux: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk @phansch, @rust-lang/infra). 💔 miri on windows: test-fail → build-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra). 💔 miri on linux: test-fail → build-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra). 💔 rls on windows: test-pass → build-fail (cc @nrc @Xanewok, @rust-lang/infra). 💔 rls on linux: test-pass → build-fail (cc @nrc @Xanewok, @rust-lang/infra).
Rustup cc rust-lang/rust#59227 (comment) This fix is obsolet once rust-lang/rust#59779 and #3926 is merged.
cc @eddyb @nnethercote