Skip to content

Commit 527292a

Browse files
committedSep 16, 2022
do not suggest a placeholder to const and static without a type
1 parent b44197a commit 527292a

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed
 

‎compiler/rustc_errors/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,10 @@ impl Handler {
637637
inner.steal((span, key)).map(|diag| DiagnosticBuilder::new_diagnostic(self, diag))
638638
}
639639

640+
pub fn has_stashed_diagnostic(&self, span: Span, key: StashKey) -> bool {
641+
self.inner.borrow().stashed_diagnostics.get(&(span, key)).is_some()
642+
}
643+
640644
/// Emit all stashed diagnostics.
641645
pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
642646
self.inner.borrow_mut().emit_stashed_diagnostics()

‎compiler/rustc_typeck/src/collect.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_ast::{MetaItemKind, NestedMetaItem};
2525
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
2626
use rustc_data_structures::captures::Captures;
2727
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
28-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
28+
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey};
2929
use rustc_hir as hir;
3030
use rustc_hir::def::{CtorKind, DefKind};
3131
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
@@ -852,12 +852,14 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
852852
tcx.ensure().type_of(trait_item_id.def_id);
853853
}
854854

855-
hir::TraitItemKind::Const(..) => {
855+
hir::TraitItemKind::Const(hir_ty, _) => {
856856
tcx.ensure().type_of(trait_item_id.def_id);
857857
// Account for `const C: _;`.
858858
let mut visitor = HirPlaceholderCollector::default();
859859
visitor.visit_trait_item(trait_item);
860-
placeholder_type_error(tcx, None, visitor.0, false, None, "constant");
860+
if !tcx.sess.diagnostic().has_stashed_diagnostic(hir_ty.span, StashKey::ItemNoType) {
861+
placeholder_type_error(tcx, None, visitor.0, false, None, "constant");
862+
}
861863
}
862864

863865
hir::TraitItemKind::Type(_, Some(_)) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
trait Foo {
2+
const A; //~ ERROR missing type for `const` item
3+
static B;
4+
//~^ ERROR associated `static` items are not allowed
5+
//~| ERROR missing type for `static` item
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: associated `static` items are not allowed
2+
--> $DIR/do-not-suggest-placeholder-to-const-static-without-type.rs:3:5
3+
|
4+
LL | static B;
5+
| ^^^^^^^^^
6+
7+
error: missing type for `const` item
8+
--> $DIR/do-not-suggest-placeholder-to-const-static-without-type.rs:2:12
9+
|
10+
LL | const A;
11+
| ^ help: provide a type for the item: `: <type>`
12+
13+
error: missing type for `static` item
14+
--> $DIR/do-not-suggest-placeholder-to-const-static-without-type.rs:3:13
15+
|
16+
LL | static B;
17+
| ^ help: provide a type for the item: `: <type>`
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)
Please sign in to comment.