Skip to content

Commit ddf2611

Browse files
committed
Auto merge of #16462 - Veykril:proc-error, r=Veykril
Better error message for when proc-macros have not yet been built Closes rust-lang/rust-analyzer#16331
2 parents 6655960 + 545382d commit ddf2611

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
359359
&'a self,
360360
ctor: &'a rustc_pattern_analysis::constructor::Constructor<Self>,
361361
ty: &'a Self::Ty,
362-
) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a> {
362+
) -> impl ExactSizeIterator<Item = Self::Ty> + Captures<'a> {
363363
let single = |ty| smallvec![ty];
364364
let tys: SmallVec<[_; 2]> = match ctor {
365365
Struct | Variant(_) | UnionField => match ty.kind(Interner) {

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,23 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
5959

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

63-
let build_text_edit = |parent_syntax, new_syntax: &SyntaxNode, old_syntax| {
65+
let build_text_edit = |new_syntax: &SyntaxNode, old_syntax| {
6466
let edit = {
67+
let old_range = ctx.sema.original_range_opt(old_syntax)?;
68+
if old_range.file_id != range.file_id {
69+
return None;
70+
}
6571
let mut builder = TextEdit::builder();
6672
if d.file.is_macro() {
6773
// we can't map the diff up into the macro input unfortunately, as the macro loses all
6874
// whitespace information so the diff wouldn't be applicable no matter what
6975
// This has the downside that the cursor will be moved in macros by doing it without a diff
7076
// but that is a trade off we can make.
7177
// FIXME: this also currently discards a lot of whitespace in the input... we really need a formatter here
72-
let range = ctx.sema.original_range_opt(old_syntax)?;
73-
builder.replace(range.range, new_syntax.to_string());
78+
builder.replace(old_range.range, new_syntax.to_string());
7479
} else {
7580
algo::diff(old_syntax, new_syntax).into_text_edit(&mut builder);
7681
}
@@ -79,8 +84,8 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
7984
Some(vec![fix(
8085
"fill_missing_fields",
8186
"Fill struct fields",
82-
SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
83-
ctx.sema.original_range(parent_syntax).range,
87+
SourceChange::from_text_edit(range.file_id, edit),
88+
range.range,
8489
)])
8590
};
8691

@@ -143,11 +148,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
143148
);
144149
new_field_list.add_field(field.clone_for_update());
145150
}
146-
build_text_edit(
147-
field_list_parent.syntax(),
148-
new_field_list.syntax(),
149-
old_field_list.syntax(),
150-
)
151+
build_text_edit(new_field_list.syntax(), old_field_list.syntax())
151152
}
152153
Either::Right(field_list_parent) => {
153154
let missing_fields = ctx.sema.record_pattern_missing_fields(field_list_parent);
@@ -160,11 +161,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
160161
));
161162
new_field_list.add_field(field.clone_for_update());
162163
}
163-
build_text_edit(
164-
field_list_parent.syntax(),
165-
new_field_list.syntax(),
166-
old_field_list.syntax(),
167-
)
164+
build_text_edit(new_field_list.syntax(), old_field_list.syntax())
168165
}
169166
}
170167
}

crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn unresolved_proc_macro(
3232
let severity = if config_enabled { Severity::Error } else { Severity::WeakWarning };
3333
let def_map = ctx.sema.db.crate_def_map(d.krate);
3434
let message = if config_enabled {
35-
def_map.proc_macro_loading_error().unwrap_or("proc macro not found in the built dylib")
35+
def_map.proc_macro_loading_error().unwrap_or("internal error")
3636
} else {
3737
match d.kind {
3838
hir::MacroKind::Attr if proc_macros_enabled => "attribute macro expansion is disabled",

crates/rust-analyzer/src/reload.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,16 @@ impl GlobalState {
528528
(crate_graph, proc_macros, crate_graph_file_dependencies)
529529
};
530530

531+
let mut change = Change::new();
531532
if self.config.expand_proc_macros() {
533+
change.set_proc_macros(
534+
crate_graph
535+
.iter()
536+
.map(|id| (id, Err("Proc-macros have not been built yet".to_owned())))
537+
.collect(),
538+
);
532539
self.fetch_proc_macros_queue.request_op(cause, proc_macro_paths);
533540
}
534-
let mut change = Change::new();
535541
change.set_crate_graph(crate_graph);
536542
self.analysis_host.apply_change(change);
537543
self.crate_graph_file_dependencies = crate_graph_file_dependencies;

0 commit comments

Comments
 (0)