Report syntax errors from item level macro expansions

This commit is contained in:
Lukas Wirth 2023-04-16 17:22:06 +02:00
parent 71b50f9f09
commit d1632c2727
11 changed files with 147 additions and 76 deletions

View file

@ -100,7 +100,10 @@ pub trait ExpandDatabase: SourceDatabase {
#[salsa::transparent]
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
#[salsa::transparent]
fn parse_or_expand_with_err(&self, file_id: HirFileId) -> Option<Parse<SyntaxNode>>;
fn parse_or_expand_with_err(
&self,
file_id: HirFileId,
) -> ExpandResult<Option<Parse<SyntaxNode>>>;
/// Implementation for the macro case.
fn parse_macro_expansion(
&self,
@ -262,11 +265,11 @@ fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> Option<Syntax
fn parse_or_expand_with_err(
db: &dyn ExpandDatabase,
file_id: HirFileId,
) -> Option<Parse<SyntaxNode>> {
) -> ExpandResult<Option<Parse<SyntaxNode>>> {
match file_id.repr() {
HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).to_syntax()),
HirFileIdRepr::FileId(file_id) => ExpandResult::ok(Some(db.parse(file_id).to_syntax())),
HirFileIdRepr::MacroFile(macro_file) => {
db.parse_macro_expansion(macro_file).value.map(|(parse, _)| parse)
db.parse_macro_expansion(macro_file).map(|it| it.map(|(parse, _)| parse))
}
}
}

View file

@ -21,7 +21,7 @@
use std::sync::Arc;
use base_db::CrateId;
use syntax::{ted, SyntaxNode};
use syntax::{ted, Parse, SyntaxNode};
use crate::{
ast::{self, AstNode},
@ -111,7 +111,7 @@ fn lazy_expand(
def: &MacroDefId,
macro_call: InFile<ast::MacroCall>,
krate: CrateId,
) -> ExpandResult<Option<InFile<SyntaxNode>>> {
) -> ExpandResult<Option<InFile<Parse<SyntaxNode>>>> {
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
let expand_to = ExpandTo::from_call_site(&macro_call.value);
@ -121,13 +121,8 @@ fn lazy_expand(
MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id), expand_to },
);
let err = db.macro_expand_error(id);
let value =
db.parse_or_expand_with_err(id.as_file()).map(|node| InFile::new(id.as_file(), node));
// FIXME: report parse errors
let value = value.map(|it| it.map(|it| it.syntax_node()));
ExpandResult { value, err }
db.parse_or_expand_with_err(id.as_file())
.map(|parse| parse.map(|parse| InFile::new(id.as_file(), parse)))
}
fn eager_macro_recur(
@ -183,8 +178,14 @@ fn eager_macro_recur(
Some(val) => {
// replace macro inside
let hygiene = Hygiene::new(db, val.file_id);
let ExpandResult { value, err: error } =
eager_macro_recur(db, &hygiene, val, krate, macro_resolver)?;
let ExpandResult { value, err: error } = eager_macro_recur(
db,
&hygiene,
// FIXME: We discard parse errors here
val.map(|it| it.syntax_node()),
krate,
macro_resolver,
)?;
let err = if err.is_none() { error } else { err };
ExpandResult { value, err }
}