Skip to content

Better error message for when proc-macros have not yet been built #16462

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2024
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-ty/src/diagnostics/match_check/pat_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
&'a self,
ctor: &'a rustc_pattern_analysis::constructor::Constructor<Self>,
ty: &'a Self::Ty,
) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a> {
) -> impl ExactSizeIterator<Item = Self::Ty> + Captures<'a> {
let single = |ty| smallvec![ty];
let tys: SmallVec<[_; 2]> = match ctor {
Struct | Variant(_) | UnionField => match ty.kind(Interner) {
Expand Down
27 changes: 12 additions & 15 deletions crates/ide-diagnostics/src/handlers/missing_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,23 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass

let current_module =
ctx.sema.scope(d.field_list_parent.to_node(&root).syntax()).map(|it| it.module());
let range = InFile::new(d.file, d.field_list_parent.text_range())
.original_node_file_range_rooted(ctx.sema.db);

let build_text_edit = |parent_syntax, new_syntax: &SyntaxNode, old_syntax| {
let build_text_edit = |new_syntax: &SyntaxNode, old_syntax| {
let edit = {
let old_range = ctx.sema.original_range_opt(old_syntax)?;
if old_range.file_id != range.file_id {
return None;
}
let mut builder = TextEdit::builder();
if d.file.is_macro() {
// we can't map the diff up into the macro input unfortunately, as the macro loses all
// whitespace information so the diff wouldn't be applicable no matter what
// This has the downside that the cursor will be moved in macros by doing it without a diff
// but that is a trade off we can make.
// FIXME: this also currently discards a lot of whitespace in the input... we really need a formatter here
let range = ctx.sema.original_range_opt(old_syntax)?;
builder.replace(range.range, new_syntax.to_string());
builder.replace(old_range.range, new_syntax.to_string());
} else {
algo::diff(old_syntax, new_syntax).into_text_edit(&mut builder);
}
Expand All @@ -79,8 +84,8 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
Some(vec![fix(
"fill_missing_fields",
"Fill struct fields",
SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
ctx.sema.original_range(parent_syntax).range,
SourceChange::from_text_edit(range.file_id, edit),
range.range,
)])
};

Expand Down Expand Up @@ -143,11 +148,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
);
new_field_list.add_field(field.clone_for_update());
}
build_text_edit(
field_list_parent.syntax(),
new_field_list.syntax(),
old_field_list.syntax(),
)
build_text_edit(new_field_list.syntax(), old_field_list.syntax())
}
Either::Right(field_list_parent) => {
let missing_fields = ctx.sema.record_pattern_missing_fields(field_list_parent);
Expand All @@ -160,11 +161,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
));
new_field_list.add_field(field.clone_for_update());
}
build_text_edit(
field_list_parent.syntax(),
new_field_list.syntax(),
old_field_list.syntax(),
)
build_text_edit(new_field_list.syntax(), old_field_list.syntax())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) fn unresolved_proc_macro(
let severity = if config_enabled { Severity::Error } else { Severity::WeakWarning };
let def_map = ctx.sema.db.crate_def_map(d.krate);
let message = if config_enabled {
def_map.proc_macro_loading_error().unwrap_or("proc macro not found in the built dylib")
def_map.proc_macro_loading_error().unwrap_or("internal error")
} else {
match d.kind {
hir::MacroKind::Attr if proc_macros_enabled => "attribute macro expansion is disabled",
Expand Down
8 changes: 7 additions & 1 deletion crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,16 @@ impl GlobalState {
(crate_graph, proc_macros, crate_graph_file_dependencies)
};

let mut change = Change::new();
if self.config.expand_proc_macros() {
change.set_proc_macros(
crate_graph
.iter()
.map(|id| (id, Err("Proc-macros have not been built yet".to_owned())))
.collect(),
);
self.fetch_proc_macros_queue.request_op(cause, proc_macro_paths);
}
let mut change = Change::new();
change.set_crate_graph(crate_graph);
self.analysis_host.apply_change(change);
self.crate_graph_file_dependencies = crate_graph_file_dependencies;
Expand Down