Skip to content

Commit 7dcd42d

Browse files
committed
Report mixed in-band on AST.
1 parent 6380193 commit 7dcd42d

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

compiler/rustc_resolve/src/late.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,28 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11441144
) -> T {
11451145
self.lifetime_ribs.push(LifetimeRib::new(kind));
11461146
let ret = work(self);
1147-
self.lifetime_ribs.pop();
1147+
let rib = self.lifetime_ribs.pop().unwrap();
1148+
if matches!(rib.kind, LifetimeRibKind::Generics { kind, .. } if kind.allow_in_band()) {
1149+
let in_band = rib
1150+
.bindings
1151+
.iter()
1152+
.find(|(_, region)| matches!(region, LifetimeRes::Param { in_band: false, .. }));
1153+
let explicit = rib.bindings.iter().find(|(_, region)| {
1154+
matches!(region, LifetimeRes::Param { in_band: true, fresh: None, .. })
1155+
});
1156+
1157+
if let (Some((explicit, _)), Some((in_band, _))) = (explicit, in_band) {
1158+
rustc_errors::struct_span_err!(
1159+
self.r.session,
1160+
in_band.span,
1161+
E0688,
1162+
"cannot mix in-band and explicit lifetime definitions"
1163+
)
1164+
.span_label(in_band.span, "in-band lifetime definition here")
1165+
.span_label(explicit.span, "explicit lifetime definition here")
1166+
.emit();
1167+
}
1168+
}
11481169
ret
11491170
}
11501171

compiler/rustc_resolve/src/late/lifetimes.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir::def_id::{DefIdMap, LocalDefId};
1616
use rustc_hir::hir_id::ItemLocalId;
1717
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1818
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node, ParamName, QPath};
19-
use rustc_hir::{GenericParamKind, HirIdMap, HirIdSet, LifetimeParamKind};
19+
use rustc_hir::{GenericParamKind, HirIdMap, HirIdSet};
2020
use rustc_middle::hir::map::Map;
2121
use rustc_middle::middle::resolve_lifetime::*;
2222
use rustc_middle::ty::{self, DefIdTree, GenericParamDefKind, TyCtxt};
@@ -1326,9 +1326,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
13261326
}
13271327

13281328
fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
1329-
if !self.trait_definition_only {
1330-
check_mixed_explicit_and_in_band_defs(self.tcx, &generics.params);
1331-
}
13321329
let scope = Scope::TraitRefBoundary { s: self.scope };
13331330
self.with(scope, |this| {
13341331
for param in generics.params {
@@ -1497,30 +1494,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
14971494
}
14981495
}
14991496

1500-
fn check_mixed_explicit_and_in_band_defs(tcx: TyCtxt<'_>, params: &[hir::GenericParam<'_>]) {
1501-
let lifetime_params: Vec<_> = params
1502-
.iter()
1503-
.filter_map(|param| match param.kind {
1504-
GenericParamKind::Lifetime { kind, .. } => Some((kind, param.span)),
1505-
_ => None,
1506-
})
1507-
.collect();
1508-
let explicit = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::Explicit);
1509-
let in_band = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::InBand);
1510-
1511-
if let (Some((_, explicit_span)), Some((_, in_band_span))) = (explicit, in_band) {
1512-
struct_span_err!(
1513-
tcx.sess,
1514-
*in_band_span,
1515-
E0688,
1516-
"cannot mix in-band and explicit lifetime definitions"
1517-
)
1518-
.span_label(*in_band_span, "in-band lifetime definition here")
1519-
.span_label(*explicit_span, "explicit lifetime definition here")
1520-
.emit();
1521-
}
1522-
}
1523-
15241497
fn compute_object_lifetime_defaults(
15251498
tcx: TyCtxt<'_>,
15261499
item: &hir::Item<'_>,

0 commit comments

Comments
 (0)