mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Fix resolution of eager macro contents
This commit is contained in:
parent
1f0c20e8ba
commit
35e5c3b3f9
12 changed files with 375 additions and 305 deletions
|
@ -27,8 +27,10 @@ use syntax::{ted, SyntaxNode};
|
|||
use crate::{
|
||||
ast::{self, AstNode},
|
||||
db::AstDatabase,
|
||||
hygiene::Hygiene,
|
||||
mod_path::ModPath,
|
||||
EagerCallInfo, ExpandTo, InFile, MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId,
|
||||
MacroDefKind,
|
||||
MacroDefKind, UnresolvedMacro,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -94,18 +96,14 @@ impl ErrorSink for &'_ mut dyn FnMut(mbe::ExpandError) {
|
|||
}
|
||||
}
|
||||
|
||||
fn err(msg: impl Into<String>) -> mbe::ExpandError {
|
||||
mbe::ExpandError::Other(msg.into())
|
||||
}
|
||||
|
||||
pub fn expand_eager_macro(
|
||||
db: &dyn AstDatabase,
|
||||
krate: CrateId,
|
||||
macro_call: InFile<ast::MacroCall>,
|
||||
def: MacroDefId,
|
||||
resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
|
||||
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||
diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
|
||||
) -> Result<MacroCallId, ErrorEmitted> {
|
||||
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
|
||||
let parsed_args = macro_call
|
||||
.value
|
||||
.token_tree()
|
||||
|
@ -129,16 +127,19 @@ pub fn expand_eager_macro(
|
|||
}),
|
||||
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
|
||||
});
|
||||
let arg_file_id = arg_id;
|
||||
|
||||
let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0;
|
||||
let result = eager_macro_recur(
|
||||
let result = match eager_macro_recur(
|
||||
db,
|
||||
InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()),
|
||||
InFile::new(arg_id.as_file(), parsed_args.syntax_node()),
|
||||
krate,
|
||||
resolver,
|
||||
diagnostic_sink,
|
||||
)?;
|
||||
) {
|
||||
Ok(Ok(it)) => it,
|
||||
Ok(Err(err)) => return Ok(Err(err)),
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
let subtree = to_subtree(&result);
|
||||
|
||||
if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
|
||||
|
@ -157,7 +158,7 @@ pub fn expand_eager_macro(
|
|||
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
|
||||
};
|
||||
|
||||
Ok(db.intern_macro_call(loc))
|
||||
Ok(Ok(db.intern_macro_call(loc)))
|
||||
} else {
|
||||
panic!("called `expand_eager_macro` on non-eager macro def {:?}", def);
|
||||
}
|
||||
|
@ -194,9 +195,10 @@ fn eager_macro_recur(
|
|||
db: &dyn AstDatabase,
|
||||
curr: InFile<SyntaxNode>,
|
||||
krate: CrateId,
|
||||
macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
|
||||
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||
mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
|
||||
) -> Result<SyntaxNode, ErrorEmitted> {
|
||||
) -> Result<Result<SyntaxNode, ErrorEmitted>, UnresolvedMacro> {
|
||||
let hygiene = Hygiene::new(db, curr.file_id);
|
||||
let original = curr.value.clone_for_update();
|
||||
|
||||
let children = original.descendants().filter_map(ast::MacroCall::cast);
|
||||
|
@ -204,23 +206,27 @@ fn eager_macro_recur(
|
|||
|
||||
// Collect replacement
|
||||
for child in children {
|
||||
let def = diagnostic_sink.option_with(
|
||||
|| macro_resolver(child.path()?),
|
||||
|| {
|
||||
let path = child.path().map(|path| format!(" `{}!`", path)).unwrap_or_default();
|
||||
err(format!("failed to resolve macro{}", path))
|
||||
},
|
||||
)?;
|
||||
let def = match child.path().and_then(|path| ModPath::from_src(db, path, &hygiene)) {
|
||||
Some(path) => macro_resolver(path.clone()).ok_or_else(|| UnresolvedMacro { path })?,
|
||||
None => {
|
||||
diagnostic_sink(mbe::ExpandError::Other("malformed macro invocation".into()));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let insert = match def.kind {
|
||||
MacroDefKind::BuiltInEager(..) => {
|
||||
let id = expand_eager_macro(
|
||||
let id = match expand_eager_macro(
|
||||
db,
|
||||
krate,
|
||||
curr.with_value(child.clone()),
|
||||
def,
|
||||
macro_resolver,
|
||||
diagnostic_sink,
|
||||
)?;
|
||||
) {
|
||||
Ok(Ok(it)) => it,
|
||||
Ok(Err(err)) => return Ok(Err(err)),
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
db.parse_or_expand(id.as_file())
|
||||
.expect("successful macro expansion should be parseable")
|
||||
.clone_for_update()
|
||||
|
@ -231,21 +237,28 @@ fn eager_macro_recur(
|
|||
| MacroDefKind::BuiltInDerive(..)
|
||||
| MacroDefKind::ProcMacro(..) => {
|
||||
let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
|
||||
let val = diagnostic_sink.expand_result_option(res)?;
|
||||
let val = match diagnostic_sink.expand_result_option(res) {
|
||||
Ok(it) => it,
|
||||
Err(err) => return Ok(Err(err)),
|
||||
};
|
||||
|
||||
// replace macro inside
|
||||
eager_macro_recur(db, val, krate, macro_resolver, diagnostic_sink)?
|
||||
match eager_macro_recur(db, val, krate, macro_resolver, diagnostic_sink) {
|
||||
Ok(Ok(it)) => it,
|
||||
Ok(Err(err)) => return Ok(Err(err)),
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// check if the whole original syntax is replaced
|
||||
if child.syntax() == &original {
|
||||
return Ok(insert);
|
||||
return Ok(Ok(insert));
|
||||
}
|
||||
|
||||
replacements.push((child, insert));
|
||||
}
|
||||
|
||||
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
||||
Ok(original)
|
||||
Ok(Ok(original))
|
||||
}
|
||||
|
|
|
@ -14,11 +14,13 @@ pub mod builtin_fn_macro;
|
|||
pub mod proc_macro;
|
||||
pub mod quote;
|
||||
pub mod eager;
|
||||
pub mod mod_path;
|
||||
|
||||
use base_db::ProcMacroKind;
|
||||
use either::Either;
|
||||
|
||||
pub use mbe::{ExpandError, ExpandResult, Origin};
|
||||
use mod_path::ModPath;
|
||||
|
||||
use std::{hash::Hash, iter, sync::Arc};
|
||||
|
||||
|
@ -835,3 +837,8 @@ impl ExpandTo {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnresolvedMacro {
|
||||
pub path: ModPath,
|
||||
}
|
||||
|
|
238
crates/hir_expand/src/mod_path.rs
Normal file
238
crates/hir_expand/src/mod_path.rs
Normal file
|
@ -0,0 +1,238 @@
|
|||
//! A lowering for `use`-paths (more generally, paths without angle-bracketed segments).
|
||||
|
||||
use std::{
|
||||
fmt::{self, Display},
|
||||
iter,
|
||||
};
|
||||
|
||||
use crate::{db::AstDatabase, hygiene::Hygiene, name::Name};
|
||||
use base_db::CrateId;
|
||||
use either::Either;
|
||||
use syntax::{ast, AstNode};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct ModPath {
|
||||
pub kind: PathKind,
|
||||
segments: Vec<Name>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum PathKind {
|
||||
Plain,
|
||||
/// `self::` is `Super(0)`
|
||||
Super(u8),
|
||||
Crate,
|
||||
/// Absolute path (::foo)
|
||||
Abs,
|
||||
/// `$crate` from macro expansion
|
||||
DollarCrate(CrateId),
|
||||
}
|
||||
|
||||
impl ModPath {
|
||||
pub fn from_src(db: &dyn AstDatabase, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
|
||||
convert_path(db, None, path, hygiene)
|
||||
}
|
||||
|
||||
pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
|
||||
let segments = segments.into_iter().collect::<Vec<_>>();
|
||||
ModPath { kind, segments }
|
||||
}
|
||||
|
||||
/// Creates a `ModPath` from a `PathKind`, with no extra path segments.
|
||||
pub const fn from_kind(kind: PathKind) -> ModPath {
|
||||
ModPath { kind, segments: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn segments(&self) -> &[Name] {
|
||||
&self.segments
|
||||
}
|
||||
|
||||
pub fn push_segment(&mut self, segment: Name) {
|
||||
self.segments.push(segment);
|
||||
}
|
||||
|
||||
pub fn pop_segment(&mut self) -> Option<Name> {
|
||||
self.segments.pop()
|
||||
}
|
||||
|
||||
/// Returns the number of segments in the path (counting special segments like `$crate` and
|
||||
/// `super`).
|
||||
pub fn len(&self) -> usize {
|
||||
self.segments.len()
|
||||
+ match self.kind {
|
||||
PathKind::Plain => 0,
|
||||
PathKind::Super(i) => i as usize,
|
||||
PathKind::Crate => 1,
|
||||
PathKind::Abs => 0,
|
||||
PathKind::DollarCrate(_) => 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_ident(&self) -> bool {
|
||||
self.as_ident().is_some()
|
||||
}
|
||||
|
||||
pub fn is_self(&self) -> bool {
|
||||
self.kind == PathKind::Super(0) && self.segments.is_empty()
|
||||
}
|
||||
|
||||
/// If this path is a single identifier, like `foo`, return its name.
|
||||
pub fn as_ident(&self) -> Option<&Name> {
|
||||
if self.kind != PathKind::Plain {
|
||||
return None;
|
||||
}
|
||||
|
||||
match &*self.segments {
|
||||
[name] => Some(name),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ModPath {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut first_segment = true;
|
||||
let mut add_segment = |s| -> fmt::Result {
|
||||
if !first_segment {
|
||||
f.write_str("::")?;
|
||||
}
|
||||
first_segment = false;
|
||||
f.write_str(s)?;
|
||||
Ok(())
|
||||
};
|
||||
match self.kind {
|
||||
PathKind::Plain => {}
|
||||
PathKind::Super(0) => add_segment("self")?,
|
||||
PathKind::Super(n) => {
|
||||
for _ in 0..n {
|
||||
add_segment("super")?;
|
||||
}
|
||||
}
|
||||
PathKind::Crate => add_segment("crate")?,
|
||||
PathKind::Abs => add_segment("")?,
|
||||
PathKind::DollarCrate(_) => add_segment("$crate")?,
|
||||
}
|
||||
for segment in &self.segments {
|
||||
if !first_segment {
|
||||
f.write_str("::")?;
|
||||
}
|
||||
first_segment = false;
|
||||
write!(f, "{}", segment)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Name> for ModPath {
|
||||
fn from(name: Name) -> ModPath {
|
||||
ModPath::from_segments(PathKind::Plain, iter::once(name))
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_path(
|
||||
db: &dyn AstDatabase,
|
||||
prefix: Option<ModPath>,
|
||||
path: ast::Path,
|
||||
hygiene: &Hygiene,
|
||||
) -> Option<ModPath> {
|
||||
let prefix = match path.qualifier() {
|
||||
Some(qual) => Some(convert_path(db, prefix, qual, hygiene)?),
|
||||
None => prefix,
|
||||
};
|
||||
|
||||
let segment = path.segment()?;
|
||||
let mut mod_path = match segment.kind()? {
|
||||
ast::PathSegmentKind::Name(name_ref) => {
|
||||
match hygiene.name_ref_to_name(db, name_ref) {
|
||||
Either::Left(name) => {
|
||||
// no type args in use
|
||||
let mut res = prefix.unwrap_or_else(|| {
|
||||
ModPath::from_kind(
|
||||
segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
|
||||
)
|
||||
});
|
||||
res.segments.push(name);
|
||||
res
|
||||
}
|
||||
Either::Right(crate_id) => {
|
||||
return Some(ModPath::from_segments(
|
||||
PathKind::DollarCrate(crate_id),
|
||||
iter::empty(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::PathSegmentKind::CrateKw => {
|
||||
if prefix.is_some() {
|
||||
return None;
|
||||
}
|
||||
ModPath::from_segments(PathKind::Crate, iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::SelfKw => {
|
||||
if prefix.is_some() {
|
||||
return None;
|
||||
}
|
||||
ModPath::from_segments(PathKind::Super(0), iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::SuperKw => {
|
||||
let nested_super_count = match prefix.map(|p| p.kind) {
|
||||
Some(PathKind::Super(n)) => n,
|
||||
Some(_) => return None,
|
||||
None => 0,
|
||||
};
|
||||
|
||||
ModPath::from_segments(PathKind::Super(nested_super_count + 1), iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::Type { .. } => {
|
||||
// not allowed in imports
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
// handle local_inner_macros :
|
||||
// Basically, even in rustc it is quite hacky:
|
||||
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
|
||||
// We follow what it did anyway :)
|
||||
if mod_path.segments.len() == 1 && mod_path.kind == PathKind::Plain {
|
||||
if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
|
||||
if let Some(crate_id) = hygiene.local_inner_macros(db, path) {
|
||||
mod_path.kind = PathKind::DollarCrate(crate_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(mod_path)
|
||||
}
|
||||
|
||||
pub use crate::name as __name;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! __known_path {
|
||||
(core::iter::IntoIterator) => {};
|
||||
(core::iter::Iterator) => {};
|
||||
(core::result::Result) => {};
|
||||
(core::option::Option) => {};
|
||||
(core::ops::Range) => {};
|
||||
(core::ops::RangeFrom) => {};
|
||||
(core::ops::RangeFull) => {};
|
||||
(core::ops::RangeTo) => {};
|
||||
(core::ops::RangeToInclusive) => {};
|
||||
(core::ops::RangeInclusive) => {};
|
||||
(core::future::Future) => {};
|
||||
(core::ops::Try) => {};
|
||||
($path:path) => {
|
||||
compile_error!("Please register your known path in the path module")
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! __path {
|
||||
($start:ident $(:: $seg:ident)*) => ({
|
||||
$crate::__known_path!($start $(:: $seg)*);
|
||||
$crate::mod_path::ModPath::from_segments($crate::mod_path::PathKind::Abs, vec![
|
||||
$crate::mod_path::__name![$start], $($crate::mod_path::__name![$seg],)*
|
||||
])
|
||||
});
|
||||
}
|
||||
|
||||
pub use crate::__path as path;
|
Loading…
Add table
Add a link
Reference in a new issue