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

Fix lifetime on LocalInternedString::get function #59227

Merged
merged 2 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ impl<'a> State<'a> {
}
hir::ItemKind::GlobalAsm(ref ga) => {
self.head(visibility_qualified(&item.vis, "global asm"))?;
self.s.word(ga.asm.as_str().get())?;
self.s.word(ga.asm.as_str().to_string())?;
self.end()?
}
hir::ItemKind::Ty(ref ty, ref generics) => {
Expand Down Expand Up @@ -1591,7 +1591,7 @@ impl<'a> State<'a> {
if ident.is_raw_guess() {
self.s.word(format!("r#{}", ident.name))?;
} else {
self.s.word(ident.as_str().get())?;
self.s.word(ident.as_str().to_string())?;
}
self.ann.post(self, AnnNode::Name(&ident.name))
}
Expand Down Expand Up @@ -1998,7 +1998,7 @@ impl<'a> State<'a> {
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
s.ibox(indent_unit)?;
if let Some(arg_name) = arg_names.get(i) {
s.s.word(arg_name.as_str().get())?;
s.s.word(arg_name.as_str().to_string())?;
s.s.word(":")?;
s.s.space()?;
} else if let Some(body_id) = body_id {
Expand Down
8 changes: 5 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2165,9 +2165,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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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 mapping on it every time would be a serious usability hit.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #59256

Some(s)
let s = s.as_str();
if s == "f32" {
Some("f32")
} else if s == "f64" {
Some("f64")
} else {
None
}
Expand Down
14 changes: 7 additions & 7 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ pub trait PrintState<'a> {
ast::LitKind::Float(ref f, t) => {
self.writer().word(format!("{}{}", &f, t.ty_to_string()))
}
ast::LitKind::FloatUnsuffixed(ref f) => self.writer().word(f.as_str().get()),
ast::LitKind::FloatUnsuffixed(ref f) => self.writer().word(f.as_str().to_string()),
ast::LitKind::Bool(val) => {
if val { self.writer().word("true") } else { self.writer().word("false") }
}
Expand Down Expand Up @@ -731,7 +731,7 @@ pub trait PrintState<'a> {
if segment.ident.name == keywords::DollarCrate.name() {
self.print_dollar_crate(segment.ident)?;
} else {
self.writer().word(segment.ident.as_str().get())?;
self.writer().word(segment.ident.as_str().to_string())?;
}
}
}
Expand All @@ -749,7 +749,7 @@ pub trait PrintState<'a> {
}
self.maybe_print_comment(attr.span.lo())?;
if attr.is_sugared_doc {
self.writer().word(attr.value_str().unwrap().as_str().get())?;
self.writer().word(attr.value_str().unwrap().as_str().to_string())?;
self.writer().hardbreak()
} else {
match attr.style {
Expand Down Expand Up @@ -858,7 +858,7 @@ pub trait PrintState<'a> {
if !ast::Ident::with_empty_ctxt(name).is_path_segment_keyword() {
self.writer().word("::")?;
}
self.writer().word(name.as_str().get())
self.writer().word(name.as_str().to_string())
}
}

Expand Down Expand Up @@ -1300,7 +1300,7 @@ impl<'a> State<'a> {
}
ast::ItemKind::GlobalAsm(ref ga) => {
self.head(visibility_qualified(&item.vis, "global_asm!"))?;
self.s.word(ga.asm.as_str().get())?;
self.s.word(ga.asm.as_str().to_string())?;
self.end()?;
}
ast::ItemKind::Ty(ref ty, ref generics) => {
Expand Down Expand Up @@ -2437,7 +2437,7 @@ impl<'a> State<'a> {
if ident.is_raw_guess() {
self.s.word(format!("r#{}", ident))?;
} else {
self.s.word(ident.as_str().get())?;
self.s.word(ident.as_str().to_string())?;
}
self.ann.post(self, AnnNode::Ident(&ident))
}
Expand All @@ -2447,7 +2447,7 @@ impl<'a> State<'a> {
}

pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
self.s.word(name.as_str().get())?;
self.s.word(name.as_str().to_string())?;
self.ann.post(self, AnnNode::Name(&name))
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ impl Ident {
}
}
fn new(sym: Symbol, is_raw: bool, span: Span) -> Ident {
let string = sym.as_str().get();
if !Self::is_valid(string) {
let string = sym.as_str();
if !Self::is_valid(&string) {
panic!("`{:?}` is not a valid identifier", string)
}
if is_raw && !ast::Ident::from_str(string).can_be_raw() {
if is_raw && !ast::Ident::from_interned_str(sym.as_interned_str()).can_be_raw() {
panic!("`{}` cannot be a raw identifier", string);
}
Ident { sym, is_raw, span }
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,11 @@ impl LocalInternedString {
}
}

pub fn get(&self) -> &'static str {
pub fn get(&self) -> &str {
Copy link
Member

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.

Copy link
Contributor Author

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.

// This returns a valid string since we ensure that `self` outlives the interner
// by creating the interner on a thread which outlives threads which can access it.
// This type cannot move to a thread which outlives the interner since it does
// not implement Send.
self.string
}
}
Expand Down