Skip to content

Commit

Permalink
Report mixed in-band on AST.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Dec 5, 2021
1 parent 6380193 commit 7dcd42d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
23 changes: 22 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,28 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
) -> T {
self.lifetime_ribs.push(LifetimeRib::new(kind));
let ret = work(self);
self.lifetime_ribs.pop();
let rib = self.lifetime_ribs.pop().unwrap();
if matches!(rib.kind, LifetimeRibKind::Generics { kind, .. } if kind.allow_in_band()) {
let in_band = rib
.bindings
.iter()
.find(|(_, region)| matches!(region, LifetimeRes::Param { in_band: false, .. }));
let explicit = rib.bindings.iter().find(|(_, region)| {
matches!(region, LifetimeRes::Param { in_band: true, fresh: None, .. })
});

if let (Some((explicit, _)), Some((in_band, _))) = (explicit, in_band) {
rustc_errors::struct_span_err!(
self.r.session,
in_band.span,
E0688,
"cannot mix in-band and explicit lifetime definitions"
)
.span_label(in_band.span, "in-band lifetime definition here")
.span_label(explicit.span, "explicit lifetime definition here")
.emit();
}
}
ret
}

Expand Down
29 changes: 1 addition & 28 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hir::def_id::{DefIdMap, LocalDefId};
use rustc_hir::hir_id::ItemLocalId;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node, ParamName, QPath};
use rustc_hir::{GenericParamKind, HirIdMap, HirIdSet, LifetimeParamKind};
use rustc_hir::{GenericParamKind, HirIdMap, HirIdSet};
use rustc_middle::hir::map::Map;
use rustc_middle::middle::resolve_lifetime::*;
use rustc_middle::ty::{self, DefIdTree, GenericParamDefKind, TyCtxt};
Expand Down Expand Up @@ -1326,9 +1326,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
}

fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
if !self.trait_definition_only {
check_mixed_explicit_and_in_band_defs(self.tcx, &generics.params);
}
let scope = Scope::TraitRefBoundary { s: self.scope };
self.with(scope, |this| {
for param in generics.params {
Expand Down Expand Up @@ -1497,30 +1494,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
}
}

fn check_mixed_explicit_and_in_band_defs(tcx: TyCtxt<'_>, params: &[hir::GenericParam<'_>]) {
let lifetime_params: Vec<_> = params
.iter()
.filter_map(|param| match param.kind {
GenericParamKind::Lifetime { kind, .. } => Some((kind, param.span)),
_ => None,
})
.collect();
let explicit = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::Explicit);
let in_band = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::InBand);

if let (Some((_, explicit_span)), Some((_, in_band_span))) = (explicit, in_band) {
struct_span_err!(
tcx.sess,
*in_band_span,
E0688,
"cannot mix in-band and explicit lifetime definitions"
)
.span_label(*in_band_span, "in-band lifetime definition here")
.span_label(*explicit_span, "explicit lifetime definition here")
.emit();
}
}

fn compute_object_lifetime_defaults(
tcx: TyCtxt<'_>,
item: &hir::Item<'_>,
Expand Down

0 comments on commit 7dcd42d

Please sign in to comment.