From 93dc6f511bedb7c18319bbf3efe47a7db4b2aa53 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 23 Oct 2020 19:27:04 +0200 Subject: Diagnose #[cfg]s in bodies --- crates/hir_def/src/body.rs | 63 ++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 42 deletions(-) (limited to 'crates/hir_def/src/body.rs') diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index d51036e4f..d10b1af01 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs @@ -1,6 +1,9 @@ //! Defines `Body`: a lowered representation of bodies of functions, statics and //! consts. mod lower; +mod diagnostics; +#[cfg(test)] +mod tests; pub mod scope; use std::{mem, ops::Index, sync::Arc}; @@ -10,7 +13,10 @@ use base_db::CrateId; use cfg::CfgOptions; use drop_bomb::DropBomb; use either::Either; -use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId}; +use hir_expand::{ + ast_id_map::AstIdMap, diagnostics::DiagnosticSink, hygiene::Hygiene, AstId, HirFileId, InFile, + MacroDefId, +}; use rustc_hash::FxHashMap; use syntax::{ast, AstNode, AstPtr}; use test_utils::mark; @@ -150,8 +156,12 @@ impl Expander { InFile { file_id: self.current_file_id, value } } - pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool { - self.cfg_expander.is_cfg_enabled(owner) + pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { + self.cfg_expander.parse_attrs(owner) + } + + pub(crate) fn cfg_options(&self) -> &CfgOptions { + &self.cfg_expander.cfg_options } fn parse_path(&mut self, path: ast::Path) -> Option { @@ -219,6 +229,10 @@ pub struct BodySourceMap { pat_map_back: ArenaMap>, field_map: FxHashMap<(ExprId, usize), InFile>>, expansions: FxHashMap>, HirFileId>, + + /// Diagnostics accumulated during body lowering. These contain `AstPtr`s and so are stored in + /// the source map (since they're just as volatile). + diagnostics: Vec, } #[derive(Default, Debug, Eq, PartialEq, Clone, Copy)] @@ -318,45 +332,10 @@ impl BodySourceMap { pub fn field_syntax(&self, expr: ExprId, field: usize) -> InFile> { self.field_map[&(expr, field)].clone() } -} - -#[cfg(test)] -mod tests { - use base_db::{fixture::WithFixture, SourceDatabase}; - use test_utils::mark; - use crate::ModuleDefId; - - use super::*; - - fn lower(ra_fixture: &str) -> Arc { - let (db, file_id) = crate::test_db::TestDB::with_single_file(ra_fixture); - - let krate = db.crate_graph().iter().next().unwrap(); - let def_map = db.crate_def_map(krate); - let module = def_map.modules_for_file(file_id).next().unwrap(); - let module = &def_map[module]; - let fn_def = match module.scope.declarations().next().unwrap() { - ModuleDefId::FunctionId(it) => it, - _ => panic!(), - }; - - db.body(fn_def.into()) - } - - #[test] - fn your_stack_belongs_to_me() { - mark::check!(your_stack_belongs_to_me); - lower( - " -macro_rules! n_nuple { - ($e:tt) => (); - ($($rest:tt)*) => {{ - (n_nuple!($($rest)*)None,) - }}; -} -fn main() { n_nuple!(1,2,3); } -", - ); + pub(crate) fn add_diagnostics(&self, _db: &dyn DefDatabase, sink: &mut DiagnosticSink<'_>) { + for diag in &self.diagnostics { + diag.add_to(sink); + } } } -- cgit v1.2.3