Skip to content

Commit 87af5f5

Browse files
committed
Supress some lookup errors if a module contains compile_error!
The problem is that when a macro expand to `compile_error!` because its input is malformed, the actual error message from the `compile_error!` might be hidden in a long list of other messages about using items that should have otherwise been generated by the macro. So suppress error about missing items in that module.
1 parent 03ce87d commit 87af5f5

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

compiler/rustc_builtin_macros/src/compile_error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub(crate) fn expand_compile_error<'cx>(
2222
#[expect(rustc::diagnostic_outside_of_impl, reason = "diagnostic message is specified by user")]
2323
#[expect(rustc::untranslatable_diagnostic, reason = "diagnostic message is specified by user")]
2424
let guar = cx.dcx().span_err(sp, var.to_string());
25+
cx.resolver.mark_scope_with_compile_error(cx.current_expansion.lint_node_id);
2526

2627
ExpandResult::Ready(DummyResult::any(sp, guar))
2728
}

compiler/rustc_expand/src/base.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,10 @@ pub trait ResolverExpand {
12001200
/// Record the name of an opaque `Ty::ImplTrait` pre-expansion so that it can be used
12011201
/// to generate an item name later that does not reference placeholder macros.
12021202
fn insert_impl_trait_name(&mut self, id: NodeId, name: Symbol);
1203+
1204+
/// Mark the scope as having a compile error so that error for lookup in this scope
1205+
/// should be suppressed
1206+
fn mark_scope_with_compile_error(&mut self, parent_node: NodeId);
12031207
}
12041208

12051209
pub trait LintStoreExpand {

compiler/rustc_resolve/src/ident.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17181718
diag_metadata: Option<&DiagMetadata<'_>>,
17191719
) -> PathResult<'ra> {
17201720
let mut module = None;
1721-
let mut module_had_parse_errors = false;
1721+
let mut module_had_parse_errors = !self.mods_with_parse_errors.is_empty()
1722+
&& self.mods_with_parse_errors.contains(&parent_scope.module.nearest_parent_mod());
17221723
let mut allow_super = true;
17231724
let mut second_binding = None;
17241725

compiler/rustc_resolve/src/macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
167167
self.invocation_parents[&id].parent_def
168168
}
169169

170+
fn mark_scope_with_compile_error(&mut self, id: NodeId) {
171+
if let Some(id) = self.opt_local_def_id(id) {
172+
self.mods_with_parse_errors.insert(id.to_def_id());
173+
}
174+
}
175+
170176
fn resolve_dollar_crates(&self) {
171177
hygiene::update_dollar_crate_names(|ctxt| {
172178
let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
mod some_module {
2+
compile_error!("Error in a module"); //~ ERROR: Error in a module
3+
4+
fn abc() {
5+
let _: self::SomeType = self::Hello::new();
6+
let _: SomeType = Hello::new();
7+
}
8+
}
9+
10+
mod another_module {}
11+
12+
fn main() {
13+
// these errors are suppressed because of the compile_error! macro
14+
15+
let _ = some_module::some_function();
16+
let _: some_module::SomeType = some_module::Hello::new();
17+
18+
// these errors are not suppressed
19+
20+
let _ = another_module::some_function();
21+
//~^ ERROR: cannot find function `some_function` in module `another_module`
22+
let _: another_module::SomeType = another_module::Hello::new();
23+
//~^ ERROR: cannot find type `SomeType` in module `another_module`
24+
//~^^ ERROR: failed to resolve: could not find `Hello` in `another_module`
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: Error in a module
2+
--> $DIR/compile_error_macro-suppress-errors.rs:2:5
3+
|
4+
LL | compile_error!("Error in a module");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0433]: failed to resolve: could not find `Hello` in `another_module`
8+
--> $DIR/compile_error_macro-suppress-errors.rs:22:55
9+
|
10+
LL | let _: another_module::SomeType = another_module::Hello::new();
11+
| ^^^^^ could not find `Hello` in `another_module`
12+
13+
error[E0425]: cannot find function `some_function` in module `another_module`
14+
--> $DIR/compile_error_macro-suppress-errors.rs:20:29
15+
|
16+
LL | let _ = another_module::some_function();
17+
| ^^^^^^^^^^^^^ not found in `another_module`
18+
19+
error[E0412]: cannot find type `SomeType` in module `another_module`
20+
--> $DIR/compile_error_macro-suppress-errors.rs:22:28
21+
|
22+
LL | let _: another_module::SomeType = another_module::Hello::new();
23+
| ^^^^^^^^ not found in `another_module`
24+
25+
error: aborting due to 4 previous errors
26+
27+
Some errors have detailed explanations: E0412, E0425, E0433.
28+
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)