Skip to content

Commit

Permalink
Rollup merge of #101790 - TaKO8Ki:do-not-suggest-placeholder-to-const…
Browse files Browse the repository at this point in the history
…-and-static-without-type, r=compiler-errors

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

Fixes #101755
  • Loading branch information
Dylan-DPC authored Sep 17, 2022
2 parents cbd561d + 527292a commit 4c64c14
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,10 @@ impl Handler {
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()
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_ast::{MetaItemKind, NestedMetaItem};
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};
Expand Down Expand Up @@ -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(_)) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4c64c14

Please sign in to comment.