Skip to content

fix: Report proc macro errors in expressions correctly as well #12648

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/hir-def/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ pub struct SyntheticSyntax;
pub enum BodyDiagnostic {
InactiveCode { node: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions },
MacroError { node: InFile<AstPtr<ast::MacroCall>>, message: String },
UnresolvedProcMacro { node: InFile<AstPtr<ast::MacroCall>> },
UnresolvedProcMacro { node: InFile<AstPtr<ast::MacroCall>>, krate: CrateId },
UnresolvedMacroCall { node: InFile<AstPtr<ast::MacroCall>>, path: ModPath },
}

Expand Down
3 changes: 2 additions & 1 deletion crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,10 @@ impl ExprCollector<'_> {

if record_diagnostics {
match &res.err {
Some(ExpandError::UnresolvedProcMacro) => {
Some(ExpandError::UnresolvedProcMacro(krate)) => {
self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro {
node: InFile::new(outer_file, syntax_ptr),
krate: *krate,
});
}
Some(err) => {
Expand Down
14 changes: 6 additions & 8 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use itertools::Itertools;
use la_arena::Idx;
use limit::Limit;
use rustc_hash::{FxHashMap, FxHashSet};
use stdx::always;
use syntax::{ast, SmolStr};

use crate::{
Expand Down Expand Up @@ -1234,7 +1235,7 @@ impl DefCollector<'_> {
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
directive.module_id,
loc.kind,
Some(loc.def.krate),
loc.def.krate,
));
return recollect_without(self);
}
Expand All @@ -1258,7 +1259,7 @@ impl DefCollector<'_> {
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
directive.module_id,
loc.kind,
Some(loc.def.krate),
loc.def.krate,
));

return recollect_without(self);
Expand Down Expand Up @@ -1308,13 +1309,10 @@ impl DefCollector<'_> {
let err = self.db.macro_expand_error(macro_call_id);
if let Some(err) = err {
let diag = match err {
hir_expand::ExpandError::UnresolvedProcMacro => {
hir_expand::ExpandError::UnresolvedProcMacro(krate) => {
always!(krate == loc.def.krate);
// Missing proc macros are non-fatal, so they are handled specially.
DefDiagnostic::unresolved_proc_macro(
module_id,
loc.kind.clone(),
Some(loc.def.krate),
)
DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone(), loc.def.krate)
}
_ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
};
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/nameres/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum DefDiagnosticKind {

UnconfiguredCode { ast: AstId<ast::Item>, cfg: CfgExpr, opts: CfgOptions },

UnresolvedProcMacro { ast: MacroCallKind, krate: Option<CrateId> },
UnresolvedProcMacro { ast: MacroCallKind, krate: CrateId },

UnresolvedMacroCall { ast: MacroCallKind, path: ModPath },

Expand Down Expand Up @@ -85,7 +85,7 @@ impl DefDiagnostic {
pub(super) fn unresolved_proc_macro(
container: LocalModuleId,
ast: MacroCallKind,
krate: Option<CrateId>,
krate: CrateId,
) -> Self {
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedProcMacro { ast, krate } }
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub type ExpandResult<T> = ValueResult<T, ExpandError>;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ExpandError {
UnresolvedProcMacro,
UnresolvedProcMacro(CrateId),
Mbe(mbe::ExpandError),
Other(Box<str>),
}
Expand All @@ -57,7 +57,7 @@ impl From<mbe::ExpandError> for ExpandError {
impl fmt::Display for ExpandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc-macro"),
ExpandError::UnresolvedProcMacro(_) => f.write_str("unresolved proc-macro"),
ExpandError::Mbe(it) => it.fmt(f),
ExpandError::Other(it) => f.write_str(it),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ProcMacroExpander {
},
}
}
None => ExpandResult::only_err(ExpandError::UnresolvedProcMacro),
None => ExpandResult::only_err(ExpandError::UnresolvedProcMacro(self.krate)),
}
}
}
2 changes: 1 addition & 1 deletion crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub struct UnresolvedProcMacro {
pub macro_name: Option<String>,
pub kind: MacroKind,
/// The crate id of the proc-macro this macro belongs to, or `None` if the proc-macro can't be found.
pub krate: Option<CrateId>,
pub krate: CrateId,
}

#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,13 @@ impl DefWithBody {
}
.into(),
),
BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
BodyDiagnostic::UnresolvedProcMacro { node, krate } => acc.push(
UnresolvedProcMacro {
node: node.clone().map(|it| it.into()),
precise_location: None,
macro_name: None,
kind: MacroKind::ProcMacro,
krate: None,
krate: *krate,
}
.into(),
),
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ pub(crate) fn unresolved_proc_macro(
None => "proc macro not expanded".to_string(),
};
let severity = if config_enabled { Severity::Error } else { Severity::WeakWarning };
let def_map = d.krate.map(|krate| ctx.sema.db.crate_def_map(krate));
let def_map = ctx.sema.db.crate_def_map(d.krate);
let message = format!(
"{message}: {}",
if config_enabled {
match def_map.as_ref().and_then(|def_map| def_map.proc_macro_loading_error()) {
match def_map.proc_macro_loading_error() {
Some(e) => e,
None => "proc macro not found",
None => "proc macro not found in the built dylib",
}
} else {
match d.kind {
Expand Down
Loading