Rollup merge of #101790 - TaKO8Ki:do-not-suggest-placeholder-to-const-and-static-without-type, r=compiler-errors

Do not suggest a placeholder to const and static without a type

Fixes #101755
This commit is contained in:
Dylan DPC 2022-09-17 15:31:08 +05:30 committed by GitHub
commit 4c64c14420
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 3 deletions

View File

@ -635,6 +635,10 @@ pub fn steal_diagnostic(&self, span: Span, key: StashKey) -> Option<DiagnosticBu
inner.steal((span, key)).map(|diag| DiagnosticBuilder::new_diagnostic(self, diag))
}
pub fn has_stashed_diagnostic(&self, span: Span, key: StashKey) -> bool {
self.inner.borrow().stashed_diagnostics.get(&(span, key)).is_some()
}
/// Emit all stashed diagnostics.
pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
self.inner.borrow_mut().emit_stashed_diagnostics()

View File

@ -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(_)) => {

View File

@ -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() {}

View File

@ -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: `: <type>`
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: `: <type>`
error: aborting due to 3 previous errors