Skip to content

Commit 0fd64ef

Browse files
Fix macro shenanigans
1 parent 7601adb commit 0fd64ef

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

compiler/rustc_hir_analysis/src/collect/type_of.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ fn infer_placeholder_type<'tcx>(
449449
})
450450
.unwrap_or_else(|| {
451451
let mut visitor = HirPlaceholderCollector::default();
452-
if let Some(ty) = tcx.hir_node_by_def_id(def_id).ty() {
452+
let node = tcx.hir_node_by_def_id(def_id);
453+
if let Some(ty) = node.ty() {
453454
visitor.visit_ty(ty);
454455
}
455456
// If we have just one span, let's try to steal a const `_` feature error.
@@ -465,7 +466,15 @@ fn infer_placeholder_type<'tcx>(
465466
}
466467
let mut diag = bad_placeholder(cx, visitor.spans, kind);
467468

468-
if !ty.references_error() {
469+
// HACK(#69396): Stashing and stealing diagnostics does not interact
470+
// well with macros which may delay more than one diagnostic on the
471+
// same span. If this happens, we will fall through to this arm, so
472+
// we need to suppress the suggestion since it's invalid. Ideally we
473+
// would suppress the duplicated error too, but that's really hard.
474+
if span.is_empty() && span.from_expansion() {
475+
// An approximately better primary message + no suggestion...
476+
diag.primary_message("missing type for item");
477+
} else if !ty.references_error() {
469478
if let Some(ty) = ty.make_suggestable(tcx, false, None) {
470479
diag.span_suggestion_verbose(
471480
span,

tests/ui/macros/issue-69396-const-no-type-in-macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro_rules! suite {
44
const A = "A".$fn();
55
//~^ ERROR the name `A` is defined multiple times
66
//~| ERROR missing type for `const` item
7-
//~| ERROR the placeholder `_` is not allowed within types on item signatures for constants
7+
//~| ERROR missing type for item
88
)*
99
}
1010
}

tests/ui/macros/issue-69396-const-no-type-in-macro.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LL | | }
2727
|
2828
= note: this error originates in the macro `suite` (in Nightly builds, run with -Z macro-backtrace for more info)
2929

30-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
30+
error[E0121]: missing type for item
3131
--> $DIR/issue-69396-const-no-type-in-macro.rs:4:20
3232
|
3333
LL | const A = "A".$fn();
@@ -40,10 +40,6 @@ LL | | }
4040
| |_- in this macro invocation
4141
|
4242
= note: this error originates in the macro `suite` (in Nightly builds, run with -Z macro-backtrace for more info)
43-
help: replace this with a fully-specified type
44-
|
45-
LL | const Abool = "A".$fn();
46-
| ++++
4743

4844
error: aborting due to 3 previous errors
4945

0 commit comments

Comments
 (0)