Skip to content

Add DefPathData::OpaqueLifetime to avoid conflicts for remapped opaque lifetimes #140769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ pub enum DefPathData {
/// An existential `impl Trait` type node.
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
OpaqueTy,
/// Used for remapped captured lifetimes in an existential `impl Trait` type node.
OpaqueLifetime(Symbol),
/// An anonymous associated type from an RPITIT. The symbol refers to the name of the method
/// that defined the type.
AnonAssocTy(Symbol),
Expand Down Expand Up @@ -445,7 +447,8 @@ impl DefPathData {
pub fn get_opt_name(&self) -> Option<Symbol> {
use self::DefPathData::*;
match *self {
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
| OpaqueLifetime(name) => Some(name),

Impl
| ForeignMod
Expand All @@ -465,9 +468,8 @@ impl DefPathData {
fn hashed_symbol(&self) -> Option<Symbol> {
use self::DefPathData::*;
match *self {
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name) => {
Some(name)
}
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name)
| OpaqueLifetime(name) => Some(name),

Impl
| ForeignMod
Expand All @@ -486,9 +488,8 @@ impl DefPathData {
pub fn name(&self) -> DefPathDataName {
use self::DefPathData::*;
match *self {
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
DefPathDataName::Named(name)
}
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
| OpaqueLifetime(name) => DefPathDataName::Named(name),
// Note that this does not show up in user print-outs.
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
Impl => DefPathDataName::Anon { namespace: kw::Impl },
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_ast::visit::walk_list;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DisambiguatorState;
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
use rustc_hir::{
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeKind, Node,
Expand Down Expand Up @@ -1470,14 +1470,14 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
let mut captures = captures.borrow_mut();
let remapped = *captures.entry(lifetime).or_insert_with(|| {
// `opaque_def_id` is unique to the `BoundVarContext` pass which is executed once
// per `resolve_bound_vars` query. This is the only location that creates nested
// lifetime inside a opaque type. `<opaque_def_id>::LifetimeNs(..)` is thus unique
// per `resolve_bound_vars` query. This is the only location that creates
// `OpaqueLifetime` paths. `<opaque_def_id>::OpaqueLifetime(..)` is thus unique
// to this query and duplicates within the query are handled by `self.disambiguator`.
let feed = self.tcx.create_def(
opaque_def_id,
Some(ident.name),
DefKind::LifetimeParam,
None,
DefKind::LifetimeParam,
Some(DefPathData::OpaqueLifetime(ident.name)),
&mut self.disambiguator,
);
feed.def_span(ident.span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
| hir::definitions::DefPathData::Use
| hir::definitions::DefPathData::GlobalAsm
| hir::definitions::DefPathData::MacroNs(..)
| hir::definitions::DefPathData::OpaqueLifetime(..)
| hir::definitions::DefPathData::LifetimeNs(..)
| hir::definitions::DefPathData::AnonAssocTy(..) => {
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
| DefPathData::Impl
| DefPathData::MacroNs(_)
| DefPathData::LifetimeNs(_)
| DefPathData::OpaqueLifetime(_)
| DefPathData::AnonAssocTy(..) => {
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/type-alias-impl-trait/lifetime-def-path-conflict-40731.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://github.com/rust-lang/rust/issues/140731
// This tests that there's no def path conflict between the
// remapped lifetime and the lifetime present in the source.

#![feature(impl_trait_in_assoc_type)]

trait Trait<'a> {}

impl<'a> Trait<'a> for u32 {
type Opq2 = impl for<'a> Trait<'a>;
//~^ ERROR: unconstrained opaque type
//~| ERROR: type `Opq2` is not a member of trait `Trait`
//~| ERROR: lifetime name `'a` shadows a lifetime name that is already in scope
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0437]: type `Opq2` is not a member of trait `Trait`
--> $DIR/lifetime-def-path-conflict-40731.rs:10:5
|
LL | type Opq2 = impl for<'a> Trait<'a>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `Trait`

error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
--> $DIR/lifetime-def-path-conflict-40731.rs:10:26
|
LL | impl<'a> Trait<'a> for u32 {
| -- first declared here
LL | type Opq2 = impl for<'a> Trait<'a>;
| ^^ lifetime `'a` already in scope

error: unconstrained opaque type
--> $DIR/lifetime-def-path-conflict-40731.rs:10:17
|
LL | type Opq2 = impl for<'a> Trait<'a>;
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `Opq2` must be used in combination with a concrete type within the same impl

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0437, E0496.
For more information about an error, try `rustc --explain E0437`.
Loading