Skip to content

Commit 2b112ef

Browse files
committed
Auto merge of #149484 - ogoffart:fix-68838, r=petrochenkov
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. Fixes #68838
2 parents ba2a7d3 + f3e73dc commit 2b112ef

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-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
@@ -1173,6 +1173,10 @@ pub trait ResolverExpand {
11731173
/// Record the name of an opaque `Ty::ImplTrait` pre-expansion so that it can be used
11741174
/// to generate an item name later that does not reference placeholder macros.
11751175
fn insert_impl_trait_name(&mut self, id: NodeId, name: Symbol);
1176+
1177+
/// Mark the scope as having a compile error so that error for lookup in this scope
1178+
/// should be suppressed
1179+
fn mark_scope_with_compile_error(&mut self, parent_node: NodeId);
11761180
}
11771181

11781182
pub trait LintStoreExpand {

compiler/rustc_resolve/src/ident.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17111711
diag_metadata: Option<&DiagMetadata<'_>>,
17121712
) -> PathResult<'ra> {
17131713
let mut module = None;
1714-
let mut module_had_parse_errors = false;
1714+
let mut module_had_parse_errors = !self.mods_with_parse_errors.is_empty()
1715+
&& self.mods_with_parse_errors.contains(&parent_scope.module.nearest_parent_mod());
17151716
let mut allow_super = true;
17161717
let mut second_binding = None;
17171718

compiler/rustc_resolve/src/macros.rs

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

169+
fn mark_scope_with_compile_error(&mut self, id: NodeId) {
170+
if let Some(id) = self.opt_local_def_id(id)
171+
&& self.tcx.def_kind(id).is_module_like()
172+
{
173+
self.mods_with_parse_errors.insert(id.to_def_id());
174+
}
175+
}
176+
169177
fn resolve_dollar_crates(&self) {
170178
hygiene::update_dollar_crate_names(|ctxt| {
171179
let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pub mod some_module {
2+
compile_error!("Error in a module"); //~ ERROR: Error in a module
3+
4+
fn abc() -> Hello {
5+
let _: self::SomeType = self::Hello::new();
6+
let _: SomeType = Hello::new();
7+
}
8+
9+
mod inner_module {
10+
use super::Hello;
11+
use crate::another_module::NotExist; //~ ERROR: unresolved import `crate::another_module::NotExist`
12+
use crate::some_module::World;
13+
struct Foo {
14+
bar: crate::some_module::Xyz,
15+
error: self::MissingType, //~ ERROR: cannot find type `MissingType` in module `self`
16+
}
17+
}
18+
}
19+
20+
pub mod another_module {
21+
use crate::some_module::NotExist;
22+
fn error_in_this_function() {
23+
compile_error!("Error in a function"); //~ ERROR: Error in a function
24+
}
25+
}
26+
27+
fn main() {
28+
// these errors are suppressed because of the compile_error! macro
29+
30+
let _ = some_module::some_function();
31+
let _: some_module::SomeType = some_module::Hello::new();
32+
33+
// these errors are not suppressed
34+
35+
let _ = another_module::some_function();
36+
//~^ ERROR: cannot find function `some_function` in module `another_module`
37+
let _: another_module::SomeType = another_module::Hello::new();
38+
//~^ ERROR: cannot find type `SomeType` in module `another_module`
39+
//~^^ ERROR: failed to resolve: could not find `Hello` in `another_module`
40+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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: Error in a function
8+
--> $DIR/compile_error_macro-suppress-errors.rs:23:9
9+
|
10+
LL | compile_error!("Error in a function");
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0432]: unresolved import `crate::another_module::NotExist`
14+
--> $DIR/compile_error_macro-suppress-errors.rs:11:13
15+
|
16+
LL | use crate::another_module::NotExist;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `NotExist` in `another_module`
18+
19+
error[E0433]: failed to resolve: could not find `Hello` in `another_module`
20+
--> $DIR/compile_error_macro-suppress-errors.rs:37:55
21+
|
22+
LL | let _: another_module::SomeType = another_module::Hello::new();
23+
| ^^^^^ could not find `Hello` in `another_module`
24+
25+
error[E0425]: cannot find type `MissingType` in module `self`
26+
--> $DIR/compile_error_macro-suppress-errors.rs:15:26
27+
|
28+
LL | error: self::MissingType,
29+
| ^^^^^^^^^^^ not found in `self`
30+
31+
error[E0425]: cannot find function `some_function` in module `another_module`
32+
--> $DIR/compile_error_macro-suppress-errors.rs:35:29
33+
|
34+
LL | let _ = another_module::some_function();
35+
| ^^^^^^^^^^^^^ not found in `another_module`
36+
37+
error[E0425]: cannot find type `SomeType` in module `another_module`
38+
--> $DIR/compile_error_macro-suppress-errors.rs:37:28
39+
|
40+
LL | let _: another_module::SomeType = another_module::Hello::new();
41+
| ^^^^^^^^ not found in `another_module`
42+
43+
error: aborting due to 7 previous errors
44+
45+
Some errors have detailed explanations: E0425, E0432, E0433.
46+
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)