]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #58876 - estebank:numeric-lifetime, r=petrochenkov
authorMazdak Farrokhzad <twingoow@gmail.com>
Wed, 13 Mar 2019 02:33:36 +0000 (03:33 +0100)
committerGitHub <noreply@github.com>
Wed, 13 Mar 2019 02:33:36 +0000 (03:33 +0100)
Parse lifetimes that start with a number and give specific error

Fix #58786.

1  2 
src/libsyntax/parse/lexer/mod.rs

index db5b8dcda4eab7daadddc04514043510a00b683b,6d2256474a3df838cc1e2499e4e6ba170fcb18b9..01e3b2929031896a97fdcec9a0b058e8d94b3f93
@@@ -1423,15 -1423,17 +1423,17 @@@ impl<'a> StringReader<'a> 
  
                  // If the character is an ident start not followed by another single
                  // quote, then this is a lifetime name:
-                 if ident_start(Some(c2)) && !self.ch_is('\'') {
+                 if (ident_start(Some(c2)) || c2.is_numeric()) && !self.ch_is('\'') {
                      while ident_continue(self.ch) {
                          self.bump();
                      }
                      // lifetimes shouldn't end with a single quote
                      // if we find one, then this is an invalid character literal
                      if self.ch_is('\'') {
-                         self.err_span_(start_with_quote, self.next_pos,
-                                 "character literal may only contain one codepoint");
+                         self.err_span_(
+                             start_with_quote,
+                             self.next_pos,
+                             "character literal may only contain one codepoint");
                          self.bump();
                          return Ok(token::Literal(token::Err(Symbol::intern("??")), None))
  
                          self.mk_ident(&format!("'{}", lifetime_name))
                      });
  
+                     if c2.is_numeric() {
+                         // this is a recovered lifetime written `'1`, error but accept it
+                         self.err_span_(
+                             start_with_quote,
+                             self.pos,
+                             "lifetimes cannot start with a number",
+                         );
+                     }
                      return Ok(token::Lifetime(ident));
                  }
  
@@@ -1873,6 -1884,7 +1884,7 @@@ fn is_block_doc_comment(s: &str) -> boo
      res
  }
  
+ /// Determine whether `c` is a valid start for an ident.
  fn ident_start(c: Option<char>) -> bool {
      let c = match c {
          Some(c) => c,
@@@ -1920,7 -1932,7 +1932,7 @@@ mod tests 
                                                            false,
                                                            false);
          ParseSess {
 -            span_diagnostic: errors::Handler::with_emitter(true, false, Box::new(emitter)),
 +            span_diagnostic: errors::Handler::with_emitter(true, None, Box::new(emitter)),
              unstable_features: UnstableFeatures::from_environment(),
              config: CrateConfig::default(),
              included_mod_stack: Lock::new(Vec::new()),