8170: Merge bang-macros and derives in name resolution r=jonas-schievink a=jonas-schievink

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-03-23 16:24:18 +00:00 committed by GitHub
commit c88129d563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,7 +91,6 @@ pub(super) fn collect_defs(
resolved_imports: Vec::new(), resolved_imports: Vec::new(),
unexpanded_macros: Vec::new(), unexpanded_macros: Vec::new(),
unexpanded_attribute_macros: Vec::new(),
mod_dirs: FxHashMap::default(), mod_dirs: FxHashMap::default(),
cfg_options, cfg_options,
proc_macros, proc_macros,
@ -202,15 +201,14 @@ struct ImportDirective {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
struct MacroDirective { struct MacroDirective {
module_id: LocalModuleId, module_id: LocalModuleId,
ast_id: AstIdWithPath<ast::MacroCall>,
legacy: Option<MacroCallId>,
depth: usize, depth: usize,
kind: MacroDirectiveKind,
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
struct DeriveDirective { enum MacroDirectiveKind {
module_id: LocalModuleId, FnLike { ast_id: AstIdWithPath<ast::MacroCall>, legacy: Option<MacroCallId> },
ast_id: AstIdWithPath<ast::Item>, Derive { ast_id: AstIdWithPath<ast::Item> },
} }
struct DefData<'a> { struct DefData<'a> {
@ -228,7 +226,6 @@ struct DefCollector<'a> {
unresolved_imports: Vec<ImportDirective>, unresolved_imports: Vec<ImportDirective>,
resolved_imports: Vec<ImportDirective>, resolved_imports: Vec<ImportDirective>,
unexpanded_macros: Vec<MacroDirective>, unexpanded_macros: Vec<MacroDirective>,
unexpanded_attribute_macros: Vec<DeriveDirective>,
mod_dirs: FxHashMap<LocalModuleId, ModDir>, mod_dirs: FxHashMap<LocalModuleId, ModDir>,
cfg_options: &'a CfgOptions, cfg_options: &'a CfgOptions,
/// List of procedural macros defined by this crate. This is read from the dynamic library /// List of procedural macros defined by this crate. This is read from the dynamic library
@ -782,19 +779,19 @@ impl DefCollector<'_> {
fn resolve_macros(&mut self) -> ReachedFixedPoint { fn resolve_macros(&mut self) -> ReachedFixedPoint {
let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new()); let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new());
let mut attribute_macros =
std::mem::replace(&mut self.unexpanded_attribute_macros, Vec::new());
let mut resolved = Vec::new(); let mut resolved = Vec::new();
let mut res = ReachedFixedPoint::Yes; let mut res = ReachedFixedPoint::Yes;
macros.retain(|directive| { macros.retain(|directive| {
if let Some(call_id) = directive.legacy { match &directive.kind {
MacroDirectiveKind::FnLike { ast_id, legacy } => {
if let Some(call_id) = legacy {
res = ReachedFixedPoint::No; res = ReachedFixedPoint::No;
resolved.push((directive.module_id, call_id, directive.depth)); resolved.push((directive.module_id, *call_id, directive.depth));
return false; return false;
} }
match macro_call_as_call_id( match macro_call_as_call_id(
&directive.ast_id, ast_id,
self.db, self.db,
self.def_map.krate, self.def_map.krate,
|path| { |path| {
@ -816,12 +813,10 @@ impl DefCollector<'_> {
} }
Err(UnresolvedMacro) | Ok(Err(_)) => {} Err(UnresolvedMacro) | Ok(Err(_)) => {}
} }
}
true MacroDirectiveKind::Derive { ast_id } => {
}); match derive_macro_as_call_id(ast_id, self.db, self.def_map.krate, |path| {
attribute_macros.retain(|directive| { self.resolve_derive_macro(directive.module_id, &path)
match derive_macro_as_call_id(&directive.ast_id, self.db, self.def_map.krate, |path| {
self.resolve_derive_macro(&directive, &path)
}) { }) {
Ok(call_id) => { Ok(call_id) => {
resolved.push((directive.module_id, call_id, 0)); resolved.push((directive.module_id, call_id, 0));
@ -830,12 +825,12 @@ impl DefCollector<'_> {
} }
Err(UnresolvedMacro) => (), Err(UnresolvedMacro) => (),
} }
}
}
true true
}); });
self.unexpanded_macros = macros; self.unexpanded_macros = macros;
self.unexpanded_attribute_macros = attribute_macros;
for (module_id, macro_call_id, depth) in resolved { for (module_id, macro_call_id, depth) in resolved {
self.collect_macro_expansion(module_id, macro_call_id, depth); self.collect_macro_expansion(module_id, macro_call_id, depth);
@ -844,15 +839,11 @@ impl DefCollector<'_> {
res res
} }
fn resolve_derive_macro( fn resolve_derive_macro(&self, module: LocalModuleId, path: &ModPath) -> Option<MacroDefId> {
&self,
directive: &DeriveDirective,
path: &ModPath,
) -> Option<MacroDefId> {
let resolved_res = self.def_map.resolve_path_fp_with_macro( let resolved_res = self.def_map.resolve_path_fp_with_macro(
self.db, self.db,
ResolveMode::Other, ResolveMode::Other,
directive.module_id, module,
&path, &path,
BuiltinShadowMode::Module, BuiltinShadowMode::Module,
); );
@ -912,9 +903,9 @@ impl DefCollector<'_> {
// Emit diagnostics for all remaining unexpanded macros. // Emit diagnostics for all remaining unexpanded macros.
for directive in &self.unexpanded_macros { for directive in &self.unexpanded_macros {
let mut error = None; match &directive.kind {
match macro_call_as_call_id( MacroDirectiveKind::FnLike { ast_id, .. } => match macro_call_as_call_id(
&directive.ast_id, ast_id,
self.db, self.db,
self.def_map.krate, self.def_map.krate,
|path| { |path| {
@ -927,18 +918,20 @@ impl DefCollector<'_> {
); );
resolved_res.resolved_def.take_macros() resolved_res.resolved_def.take_macros()
}, },
&mut |e| { &mut |_| (),
error.get_or_insert(e);
},
) { ) {
Ok(_) => (), Ok(_) => (),
Err(UnresolvedMacro) => { Err(UnresolvedMacro) => {
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
directive.module_id, directive.module_id,
directive.ast_id.ast_id, ast_id.ast_id,
)); ));
} }
}; },
MacroDirectiveKind::Derive { .. } => {
// FIXME: we might want to diagnose this too
}
}
} }
// Emit diagnostics for all remaining unresolved imports. // Emit diagnostics for all remaining unresolved imports.
@ -1380,9 +1373,11 @@ impl ModCollector<'_, '_> {
Some(derive_macros) => { Some(derive_macros) => {
for path in derive_macros { for path in derive_macros {
let ast_id = AstIdWithPath::new(self.file_id, ast_id, path); let ast_id = AstIdWithPath::new(self.file_id, ast_id, path);
self.def_collector self.def_collector.unexpanded_macros.push(MacroDirective {
.unexpanded_attribute_macros module_id: self.module_id,
.push(DeriveDirective { module_id: self.module_id, ast_id }); depth: self.macro_depth + 1,
kind: MacroDirectiveKind::Derive { ast_id },
});
} }
} }
None => { None => {
@ -1497,9 +1492,8 @@ impl ModCollector<'_, '_> {
self.def_collector.unexpanded_macros.push(MacroDirective { self.def_collector.unexpanded_macros.push(MacroDirective {
module_id: self.module_id, module_id: self.module_id,
ast_id,
legacy: None,
depth: self.macro_depth + 1, depth: self.macro_depth + 1,
kind: MacroDirectiveKind::FnLike { ast_id, legacy: None },
}); });
} }
@ -1542,7 +1536,6 @@ mod tests {
unresolved_imports: Vec::new(), unresolved_imports: Vec::new(),
resolved_imports: Vec::new(), resolved_imports: Vec::new(),
unexpanded_macros: Vec::new(), unexpanded_macros: Vec::new(),
unexpanded_attribute_macros: Vec::new(),
mod_dirs: FxHashMap::default(), mod_dirs: FxHashMap::default(),
cfg_options: &CfgOptions::default(), cfg_options: &CfgOptions::default(),
proc_macros: Default::default(), proc_macros: Default::default(),