Auto merge of #16462 - Veykril:proc-error, r=Veykril

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

Closes https://github.com/rust-lang/rust-analyzer/issues/16331
This commit is contained in:
bors 2024-02-08 13:56:58 +00:00
commit ddf26113fc
4 changed files with 21 additions and 18 deletions

View File

@ -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) {

View File

@ -59,9 +59,15 @@ 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
@ -69,8 +75,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
// 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);
}
@ -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,
)])
};
@ -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);
@ -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())
}
}
}

View File

@ -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",

View File

@ -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;