From 527292a1a697c8831fc1ec01b1047ecebc10f809 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 16 Sep 2022 11:24:14 +0900 Subject: [PATCH] do not suggest a placeholder to const and static without a type --- compiler/rustc_errors/src/lib.rs | 4 ++++ compiler/rustc_typeck/src/collect.rs | 8 +++++--- ...laceholder-to-const-static-without-type.rs | 8 ++++++++ ...holder-to-const-static-without-type.stderr | 20 +++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.rs create mode 100644 src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.stderr diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 68abdd0bad1..d89b3da049c 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -637,6 +637,10 @@ pub fn steal_diagnostic(&self, span: Span, key: StashKey) -> Option bool { + self.inner.borrow().stashed_diagnostics.get(&(span, key)).is_some() + } + /// Emit all stashed diagnostics. pub fn emit_stashed_diagnostics(&self) -> Option { self.inner.borrow_mut().emit_stashed_diagnostics() diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 6236ad370df..8f5b4815cf8 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -25,7 +25,7 @@ use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr}; use rustc_data_structures::captures::Captures; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; -use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed}; +use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey}; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind}; 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) { tcx.ensure().type_of(trait_item_id.def_id); } - hir::TraitItemKind::Const(..) => { + hir::TraitItemKind::Const(hir_ty, _) => { tcx.ensure().type_of(trait_item_id.def_id); // Account for `const C: _;`. let mut visitor = HirPlaceholderCollector::default(); visitor.visit_trait_item(trait_item); - placeholder_type_error(tcx, None, visitor.0, false, None, "constant"); + if !tcx.sess.diagnostic().has_stashed_diagnostic(hir_ty.span, StashKey::ItemNoType) { + placeholder_type_error(tcx, None, visitor.0, false, None, "constant"); + } } hir::TraitItemKind::Type(_, Some(_)) => { diff --git a/src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.rs b/src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.rs new file mode 100644 index 00000000000..97e0b213f2e --- /dev/null +++ b/src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.rs @@ -0,0 +1,8 @@ +trait Foo { + const A; //~ ERROR missing type for `const` item + static B; + //~^ ERROR associated `static` items are not allowed + //~| ERROR missing type for `static` item +} + +fn main() {} diff --git a/src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.stderr b/src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.stderr new file mode 100644 index 00000000000..8982d628561 --- /dev/null +++ b/src/test/ui/typeck/do-not-suggest-placeholder-to-const-static-without-type.stderr @@ -0,0 +1,20 @@ +error: associated `static` items are not allowed + --> $DIR/do-not-suggest-placeholder-to-const-static-without-type.rs:3:5 + | +LL | static B; + | ^^^^^^^^^ + +error: missing type for `const` item + --> $DIR/do-not-suggest-placeholder-to-const-static-without-type.rs:2:12 + | +LL | const A; + | ^ help: provide a type for the item: `: ` + +error: missing type for `static` item + --> $DIR/do-not-suggest-placeholder-to-const-static-without-type.rs:3:13 + | +LL | static B; + | ^ help: provide a type for the item: `: ` + +error: aborting due to 3 previous errors +