]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #42006 - jseyfried:fix_include_regression, r=nrc
authorMark Simulacrum <mark.simulacrum@gmail.com>
Fri, 19 May 2017 20:16:15 +0000 (14:16 -0600)
committerGitHub <noreply@github.com>
Fri, 19 May 2017 20:16:15 +0000 (14:16 -0600)
Fix ICE on `include!(line!())` (regression)

Fixes #41776.
r? @nrc

1  2 
src/libsyntax/ext/base.rs
src/libsyntax/ext/source_util.rs

index 00483b1ea5f77f0bbcdeba3a329717a0dd2ce6b6,1f279cc9c80cdbd8cb339997071f26b5b1d56337..1930f61121bb028113f2dc37d5b037f6ca9f2a47
@@@ -24,7 -24,6 +24,7 @@@ use ptr::P
  use symbol::Symbol;
  use util::small_vector::SmallVector;
  
 +use std::collections::HashMap;
  use std::path::PathBuf;
  use std::rc::Rc;
  use std::default::Default;
@@@ -535,7 -534,7 +535,7 @@@ pub enum SyntaxExtension 
      ///
      /// The `bool` dictates whether the contents of the macro can
      /// directly use `#[unstable]` things (true == yes).
 -    NormalTT(Box<TTMacroExpander>, Option<Span>, bool),
 +    NormalTT(Box<TTMacroExpander>, Option<(ast::NodeId, Span)>, bool),
  
      /// A function-like syntax extension that has an extra ident before
      /// the block.
@@@ -589,7 -588,6 +589,7 @@@ pub trait Resolver 
                       -> Result<Option<Rc<SyntaxExtension>>, Determinacy>;
      fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
                       -> Result<Rc<SyntaxExtension>, Determinacy>;
 +    fn check_unused_macros(&self);
  }
  
  #[derive(Copy, Clone, Debug)]
@@@ -619,7 -617,6 +619,7 @@@ impl Resolver for DummyResolver 
                       _force: bool) -> Result<Rc<SyntaxExtension>, Determinacy> {
          Err(Determinacy::Determined)
      }
 +    fn check_unused_macros(&self) {}
  }
  
  #[derive(Clone)]
@@@ -637,8 -634,8 +637,8 @@@ pub struct ExpansionData 
  }
  
  /// One of these is made during expansion and incrementally updated as we go;
 -/// when a macro expansion occurs, the resulting nodes have the backtrace()
 -/// -> expn_info of their expansion context stored into their span.
 +/// when a macro expansion occurs, the resulting nodes have the `backtrace()
 +/// -> expn_info` of their expansion context stored into their span.
  pub struct ExtCtxt<'a> {
      pub parse_sess: &'a parse::ParseSess,
      pub ecfg: expand::ExpansionConfig<'a>,
      pub resolver: &'a mut Resolver,
      pub resolve_err_count: usize,
      pub current_expansion: ExpansionData,
 +    pub expansions: HashMap<Span, Vec<String>>,
  }
  
  impl<'a> ExtCtxt<'a> {
                  module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }),
                  directory_ownership: DirectoryOwnership::Owned,
              },
 +            expansions: HashMap::new(),
          }
      }
  
      /// Returns span for the macro which originally caused the current expansion to happen.
      ///
      /// Stops backtracing at include! boundary.
-     pub fn expansion_cause(&self) -> Span {
+     pub fn expansion_cause(&self) -> Option<Span> {
          let mut ctxt = self.backtrace();
          let mut last_macro = None;
          loop {
                  }
                  ctxt = info.call_site.ctxt;
                  last_macro = Some(info.call_site);
 -                return Some(());
 +                Some(())
              }).is_none() {
                  break
              }
          }
-         last_macro.expect("missing expansion backtrace")
+         last_macro
      }
  
      pub fn struct_span_warn(&self,
      pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
          self.parse_sess.span_diagnostic.span_bug(sp, msg);
      }
 +    pub fn trace_macros_diag(&self) {
 +        for (sp, notes) in self.expansions.iter() {
 +            let mut db = self.parse_sess.span_diagnostic.span_note_diag(*sp, "trace_macro");
 +            for note in notes {
 +                db.note(note);
 +            }
 +            db.emit();
 +        }
 +    }
      pub fn bug(&self, msg: &str) -> ! {
          self.parse_sess.span_diagnostic.bug(msg);
      }
              v.push(self.ident_of(s));
          }
          v.extend(components.iter().map(|s| self.ident_of(s)));
 -        return v
 +        v
      }
      pub fn name_of(&self, st: &str) -> ast::Name {
          Symbol::intern(st)
      }
 +
 +    pub fn check_unused_macros(&self) {
 +        self.resolver.check_unused_macros();
 +    }
  }
  
  /// Extract a string literal from the macro expanded version of `expr`,
index 4183583d66ffd1b34a7929b9729817ebab60dfd8,fc557a78dda0d3f0456532889ec5deeae43d9ba9..3cdd3a4b2c31d8a313c863cde1311e5b1a5c8a17
@@@ -35,7 -35,7 +35,7 @@@ pub fn expand_line(cx: &mut ExtCtxt, sp
                     -> Box<base::MacResult+'static> {
      base::check_zero_tts(cx, sp, tts, "line!");
  
-     let topmost = cx.expansion_cause();
+     let topmost = cx.expansion_cause().unwrap_or(sp);
      let loc = cx.codemap().lookup_char_pos(topmost.lo);
  
      base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
@@@ -46,7 -46,7 +46,7 @@@ pub fn expand_column(cx: &mut ExtCtxt, 
                    -> Box<base::MacResult+'static> {
      base::check_zero_tts(cx, sp, tts, "column!");
  
-     let topmost = cx.expansion_cause();
+     let topmost = cx.expansion_cause().unwrap_or(sp);
      let loc = cx.codemap().lookup_char_pos(topmost.lo);
  
      base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32))
@@@ -59,7 -59,7 +59,7 @@@ pub fn expand_file(cx: &mut ExtCtxt, sp
                     -> Box<base::MacResult+'static> {
      base::check_zero_tts(cx, sp, tts, "file!");
  
-     let topmost = cx.expansion_cause();
+     let topmost = cx.expansion_cause().unwrap_or(sp);
      let loc = cx.codemap().lookup_char_pos(topmost.lo);
      base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name)))
  }
@@@ -150,7 -150,7 +150,7 @@@ pub fn expand_include_str(cx: &mut ExtC
              cx.span_err(sp,
                          &format!("{} wasn't a utf-8 file",
                                  file.display()));
 -            return DummyResult::expr(sp);
 +            DummyResult::expr(sp)
          }
      }
  }
@@@ -167,7 -167,7 +167,7 @@@ pub fn expand_include_bytes(cx: &mut Ex
          Err(e) => {
              cx.span_err(sp,
                          &format!("couldn't read {}: {}", file.display(), e));
 -            return DummyResult::expr(sp);
 +            DummyResult::expr(sp)
          }
          Ok(..) => {
              // Add this input file to the code map to make it available as