Skip to content

Commit 438f6b0

Browse files
committed
Fix lifetime on LocalInternedString::get function
1 parent befeeb7 commit 438f6b0

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/libsyntax/parse/parser.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2165,9 +2165,11 @@ impl<'a> Parser<'a> {
21652165
suffix,
21662166
) = self.token {
21672167
let suffix = suffix.and_then(|s| {
2168-
let s = s.as_str().get();
2169-
if ["f32", "f64"].contains(&s) {
2170-
Some(s)
2168+
let s = s.as_str();
2169+
if s == "f32" {
2170+
Some("f32")
2171+
} else if s == "f64" {
2172+
Some("f64")
21712173
} else {
21722174
None
21732175
}

src/libsyntax/print/pp.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<'a> Printer<'a> {
369369
Ok(())
370370
}
371371

372-
fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
372+
fn pretty_print_string<'s>(&mut self, s: Cow<'s, str>, len: isize) -> io::Result<()> {
373373
if self.scan_stack.is_empty() {
374374
debug!("pp String('{}')/print Vec<{},{}>",
375375
s, self.left, self.right);
@@ -378,7 +378,10 @@ impl<'a> Printer<'a> {
378378
debug!("pp String('{}')/buffer Vec<{},{}>",
379379
s, self.left, self.right);
380380
self.advance_right();
381-
self.buf[self.right] = BufEntry { token: Token::String(s, len), size: len };
381+
self.buf[self.right] = BufEntry {
382+
token: Token::String(s.into_owned().into(), len),
383+
size: len
384+
};
382385
self.right_total += len;
383386
self.check_stream()
384387
}
@@ -576,7 +579,7 @@ impl<'a> Printer<'a> {
576579
}
577580
}
578581

579-
pub fn print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
582+
pub fn print_string(&mut self, s: Cow<'_, str>, len: isize) -> io::Result<()> {
580583
debug!("print String({})", s);
581584
// assert!(len <= space);
582585
self.space -= len;
@@ -641,7 +644,7 @@ impl<'a> Printer<'a> {
641644
self.pretty_print_eof()
642645
}
643646

644-
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) -> io::Result<()> {
647+
pub fn word<'s, S: Into<Cow<'s, str>>>(&mut self, wrd: S) -> io::Result<()> {
645648
let s = wrd.into();
646649
let len = s.len() as isize;
647650
self.pretty_print_string(s, len)

src/libsyntax_ext/proc_macro_server.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ impl Ident {
336336
}
337337
}
338338
fn new(sym: Symbol, is_raw: bool, span: Span) -> Ident {
339-
let string = sym.as_str().get();
340-
if !Self::is_valid(string) {
339+
let string = sym.as_str();
340+
if !Self::is_valid(&string) {
341341
panic!("`{:?}` is not a valid identifier", string)
342342
}
343-
if is_raw && !ast::Ident::from_str(string).can_be_raw() {
343+
if is_raw && !ast::Ident::from_interned_str(sym.as_interned_str()).can_be_raw() {
344344
panic!("`{}` cannot be a raw identifier", string);
345345
}
346346
Ident { sym, is_raw, span }

src/libsyntax_pos/symbol.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,11 @@ impl LocalInternedString {
524524
}
525525
}
526526

527-
pub fn get(&self) -> &'static str {
527+
pub fn get(&self) -> &str {
528+
// This returns a valid string since we ensure that `self` outlives the interner
529+
// by creating the interner on a thread which outlives threads which can access it.
530+
// This type cannot move to a thread which outlives the interner since it does
531+
// not implement Send.
528532
self.string
529533
}
530534
}

0 commit comments

Comments
 (0)