Skip to content

Commit b80749c

Browse files
authored
Rollup merge of rust-lang#140769 - Zoxc:fix-140731, r=oli-obk
Add `DefPathData::OpaqueLifetime` to avoid conflicts for remapped opaque lifetimes This adds `DefPathData::OpaqueLifetime` to ensure the def paths for remapped opaque lifetimes remain unique. Fixes rust-lang#140731. r? `@oli-obk`
2 parents 4f3afa8 + 5913e55 commit b80749c

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

compiler/rustc_hir/src/definitions.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ pub enum DefPathData {
309309
/// An existential `impl Trait` type node.
310310
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
311311
OpaqueTy,
312+
/// Used for remapped captured lifetimes in an existential `impl Trait` type node.
313+
OpaqueLifetime(Symbol),
312314
/// An anonymous associated type from an RPITIT. The symbol refers to the name of the method
313315
/// that defined the type.
314316
AnonAssocTy(Symbol),
@@ -445,7 +447,8 @@ impl DefPathData {
445447
pub fn get_opt_name(&self) -> Option<Symbol> {
446448
use self::DefPathData::*;
447449
match *self {
448-
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
450+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
451+
| OpaqueLifetime(name) => Some(name),
449452

450453
Impl
451454
| ForeignMod
@@ -465,9 +468,8 @@ impl DefPathData {
465468
fn hashed_symbol(&self) -> Option<Symbol> {
466469
use self::DefPathData::*;
467470
match *self {
468-
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name) => {
469-
Some(name)
470-
}
471+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name)
472+
| OpaqueLifetime(name) => Some(name),
471473

472474
Impl
473475
| ForeignMod
@@ -486,9 +488,8 @@ impl DefPathData {
486488
pub fn name(&self) -> DefPathDataName {
487489
use self::DefPathData::*;
488490
match *self {
489-
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
490-
DefPathDataName::Named(name)
491-
}
491+
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
492+
| OpaqueLifetime(name) => DefPathDataName::Named(name),
492493
// Note that this does not show up in user print-outs.
493494
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
494495
Impl => DefPathDataName::Anon { namespace: kw::Impl },

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::visit::walk_list;
1414
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
1515
use rustc_errors::ErrorGuaranteed;
1616
use rustc_hir::def::{DefKind, Res};
17-
use rustc_hir::definitions::DisambiguatorState;
17+
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
1818
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
1919
use rustc_hir::{
2020
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeKind, Node,
@@ -1470,14 +1470,14 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
14701470
let mut captures = captures.borrow_mut();
14711471
let remapped = *captures.entry(lifetime).or_insert_with(|| {
14721472
// `opaque_def_id` is unique to the `BoundVarContext` pass which is executed once
1473-
// per `resolve_bound_vars` query. This is the only location that creates nested
1474-
// lifetime inside a opaque type. `<opaque_def_id>::LifetimeNs(..)` is thus unique
1473+
// per `resolve_bound_vars` query. This is the only location that creates
1474+
// `OpaqueLifetime` paths. `<opaque_def_id>::OpaqueLifetime(..)` is thus unique
14751475
// to this query and duplicates within the query are handled by `self.disambiguator`.
14761476
let feed = self.tcx.create_def(
14771477
opaque_def_id,
1478-
Some(ident.name),
1479-
DefKind::LifetimeParam,
14801478
None,
1479+
DefKind::LifetimeParam,
1480+
Some(DefPathData::OpaqueLifetime(ident.name)),
14811481
&mut self.disambiguator,
14821482
);
14831483
feed.def_span(ident.span);

compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs

+1
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
722722
| hir::definitions::DefPathData::Use
723723
| hir::definitions::DefPathData::GlobalAsm
724724
| hir::definitions::DefPathData::MacroNs(..)
725+
| hir::definitions::DefPathData::OpaqueLifetime(..)
725726
| hir::definitions::DefPathData::LifetimeNs(..)
726727
| hir::definitions::DefPathData::AnonAssocTy(..) => {
727728
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);

compiler/rustc_symbol_mangling/src/v0.rs

+1
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
890890
| DefPathData::Impl
891891
| DefPathData::MacroNs(_)
892892
| DefPathData::LifetimeNs(_)
893+
| DefPathData::OpaqueLifetime(_)
893894
| DefPathData::AnonAssocTy(..) => {
894895
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
895896
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://github.com/rust-lang/rust/issues/140731
2+
// This tests that there's no def path conflict between the
3+
// remapped lifetime and the lifetime present in the source.
4+
5+
#![feature(impl_trait_in_assoc_type)]
6+
7+
trait Trait<'a> {}
8+
9+
impl<'a> Trait<'a> for u32 {
10+
type Opq2 = impl for<'a> Trait<'a>;
11+
//~^ ERROR: unconstrained opaque type
12+
//~| ERROR: type `Opq2` is not a member of trait `Trait`
13+
//~| ERROR: lifetime name `'a` shadows a lifetime name that is already in scope
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0437]: type `Opq2` is not a member of trait `Trait`
2+
--> $DIR/lifetime-def-path-conflict-40731.rs:10:5
3+
|
4+
LL | type Opq2 = impl for<'a> Trait<'a>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `Trait`
6+
7+
error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
8+
--> $DIR/lifetime-def-path-conflict-40731.rs:10:26
9+
|
10+
LL | impl<'a> Trait<'a> for u32 {
11+
| -- first declared here
12+
LL | type Opq2 = impl for<'a> Trait<'a>;
13+
| ^^ lifetime `'a` already in scope
14+
15+
error: unconstrained opaque type
16+
--> $DIR/lifetime-def-path-conflict-40731.rs:10:17
17+
|
18+
LL | type Opq2 = impl for<'a> Trait<'a>;
19+
| ^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
= note: `Opq2` must be used in combination with a concrete type within the same impl
22+
23+
error: aborting due to 3 previous errors
24+
25+
Some errors have detailed explanations: E0437, E0496.
26+
For more information about an error, try `rustc --explain E0437`.

0 commit comments

Comments
 (0)