Skip to content

Commit 7956b1c

Browse files
committed
Assoc consts don't have generics
Fix #74264.
1 parent 0f7205f commit 7956b1c

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/librustc_resolve/late/diagnostics.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::PrimTy;
1717
use rustc_session::config::nightly_options;
1818
use rustc_span::hygiene::MacroKind;
1919
use rustc_span::symbol::{kw, sym, Ident};
20-
use rustc_span::{BytePos, Span};
20+
use rustc_span::{BytePos, Span, DUMMY_SP};
2121

2222
use log::debug;
2323

@@ -1273,6 +1273,15 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
12731273
let should_break;
12741274
introduce_suggestion.push(match missing {
12751275
MissingLifetimeSpot::Generics(generics) => {
1276+
if generics.span == DUMMY_SP {
1277+
// Account for malformed generics in the HIR. This shouldn't happen,
1278+
// but if we make a mistake elsewhere, mainly by keeping something in
1279+
// `missing_named_lifetime_spots` that we shouldn't, like associated
1280+
// `const`s or making a mistake in the AST lowering we would provide
1281+
// non-sensical suggestions. Guard against that by skipping these.
1282+
// (#74264)
1283+
continue;
1284+
}
12761285
msg = "consider introducing a named lifetime parameter".to_string();
12771286
should_break = true;
12781287
if let Some(param) = generics.params.iter().find(|p| match p.kind {

src/librustc_resolve/late/lifetimes.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -711,18 +711,20 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
711711

712712
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
713713
use self::hir::TraitItemKind::*;
714-
self.missing_named_lifetime_spots.push((&trait_item.generics).into());
715714
match trait_item.kind {
716715
Fn(ref sig, _) => {
716+
self.missing_named_lifetime_spots.push((&trait_item.generics).into());
717717
let tcx = self.tcx;
718718
self.visit_early_late(
719719
Some(tcx.hir().get_parent_item(trait_item.hir_id)),
720720
&sig.decl,
721721
&trait_item.generics,
722722
|this| intravisit::walk_trait_item(this, trait_item),
723723
);
724+
self.missing_named_lifetime_spots.pop();
724725
}
725726
Type(bounds, ref ty) => {
727+
self.missing_named_lifetime_spots.push((&trait_item.generics).into());
726728
let generics = &trait_item.generics;
727729
let mut index = self.next_early_index();
728730
debug!("visit_ty: index = {}", index);
@@ -757,14 +759,14 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
757759
this.visit_ty(ty);
758760
}
759761
});
762+
self.missing_named_lifetime_spots.pop();
760763
}
761764
Const(_, _) => {
762765
// Only methods and types support generics.
763766
assert!(trait_item.generics.params.is_empty());
764767
intravisit::walk_trait_item(self, trait_item);
765768
}
766769
}
767-
self.missing_named_lifetime_spots.pop();
768770
}
769771

770772
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait ZstAssert: Sized {
2+
const TYPE_NAME: &str = ""; //~ ERROR missing lifetime specifier
3+
}
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0106]: missing lifetime specifier
2+
--> $DIR/missing-lifetime-in-assoc-const-type.rs:2:22
3+
|
4+
LL | const TYPE_NAME: &str = "";
5+
| ^ expected named lifetime parameter
6+
|
7+
help: consider introducing a named lifetime parameter
8+
|
9+
LL | trait ZstAssert<'a>: Sized {
10+
LL | const TYPE_NAME: &'a str = "";
11+
|
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)