1
Fork 0

Fix lifetime on LocalInternedString::get function

This commit is contained in:
John Kåre Alsaker 2019-03-16 08:50:19 +01:00
parent befeeb7c08
commit 438f6b04c6
4 changed files with 20 additions and 11 deletions

View file

@ -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) {
Some(s)
let s = s.as_str();
if s == "f32" {
Some("f32")
} else if s == "f64" {
Some("f64")
} else {
None
}

View file

@ -369,7 +369,7 @@ impl<'a> Printer<'a> {
Ok(())
}
fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
fn pretty_print_string<'s>(&mut self, s: Cow<'s, str>, len: isize) -> io::Result<()> {
if self.scan_stack.is_empty() {
debug!("pp String('{}')/print Vec<{},{}>",
s, self.left, self.right);
@ -378,7 +378,10 @@ impl<'a> Printer<'a> {
debug!("pp String('{}')/buffer Vec<{},{}>",
s, self.left, self.right);
self.advance_right();
self.buf[self.right] = BufEntry { token: Token::String(s, len), size: len };
self.buf[self.right] = BufEntry {
token: Token::String(s.into_owned().into(), len),
size: len
};
self.right_total += len;
self.check_stream()
}
@ -576,7 +579,7 @@ impl<'a> Printer<'a> {
}
}
pub fn print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
pub fn print_string(&mut self, s: Cow<'_, str>, len: isize) -> io::Result<()> {
debug!("print String({})", s);
// assert!(len <= space);
self.space -= len;
@ -641,7 +644,7 @@ impl<'a> Printer<'a> {
self.pretty_print_eof()
}
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) -> io::Result<()> {
pub fn word<'s, S: Into<Cow<'s, str>>>(&mut self, wrd: S) -> io::Result<()> {
let s = wrd.into();
let len = s.len() as isize;
self.pretty_print_string(s, len)

View file

@ -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 }

View file

@ -524,7 +524,11 @@ impl LocalInternedString {
}
}
pub fn get(&self) -> &'static str {
pub fn get(&self) -> &str {
// 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
}
}