Skip to content

Commit e683765

Browse files
committed
[WIP]
1 parent 25f7b80 commit e683765

File tree

7 files changed

+79
-44
lines changed

7 files changed

+79
-44
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3535
use rustc_infer::traits::{DynCompatibilityViolation, ObligationCause};
3636
use rustc_middle::query::Providers;
3737
use rustc_middle::ty::typeck_results::{
38-
HasTypeDependentDefs, LocalTableInContext, LocalTableInContextMut, TypeDependentDef,
39-
TypeDependentDefs,
38+
HasTypeDependentDefs, LocalTableInContext, LocalTableInContextMut, TypeDependentDefs,
4039
};
4140
use rustc_middle::ty::util::{Discr, IntTypeExt};
4241
use rustc_middle::ty::{
@@ -520,12 +519,12 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
520519
// There's no place to record types from signatures?
521520
}
522521

523-
fn record_res(&self, hir_id: hir::HirId, result: TypeDependentDef) {
522+
fn record_res(&self, hir_id: hir::HirId, def_id: DefId) {
524523
LocalTableInContextMut::new(
525524
self.hir_id().owner,
526525
&mut self.type_dependent_defs.borrow_mut(),
527526
)
528-
.insert(hir_id, result);
527+
.insert(hir_id, Ok((self.tcx.def_kind(def_id), def_id)));
529528
}
530529

531530
fn infcx(&self) -> Option<&InferCtxt<'tcx>> {

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
20632063
matches!(args.parenthesized, hir::GenericArgsParentheses::ReturnTypeNotation)
20642064
}) =>
20652065
{
2066+
// FIXME(fmease): No longer true.
20662067
// First, ignore a qself that isn't a type or `Self` param. Those are the
20672068
// only ones that support `T::Assoc` anyways in HIR lowering.
20682069
let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = qself.kind else {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_macros::{TypeFoldable, TypeVisitable};
3838
use rustc_middle::middle::stability::AllowUnstable;
3939
use rustc_middle::mir::interpret::LitToConstInput;
4040
use rustc_middle::ty::print::PrintPolyTraitRefExt as _;
41-
use rustc_middle::ty::typeck_results::{HasTypeDependentDefs, TypeDependentDef};
41+
use rustc_middle::ty::typeck_results::HasTypeDependentDefs;
4242
use rustc_middle::ty::{
4343
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt,
4444
TypingMode, Upcast, fold_regions,
@@ -204,7 +204,7 @@ pub trait HirTyLowerer<'tcx>: HasTypeDependentDefs {
204204
fn record_ty(&self, hir_id: HirId, ty: Ty<'tcx>, span: Span);
205205

206206
/// Record the resolution of a HIR node corresponding to a type-dependent definition in this context.
207-
fn record_res(&self, hir_id: hir::HirId, result: TypeDependentDef);
207+
fn record_res(&self, hir_id: hir::HirId, result: DefId);
208208

209209
/// The inference context of the lowering context if applicable.
210210
fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
@@ -1159,28 +1159,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11591159
/// Lower a [type-relative](hir::QPath::TypeRelative) path in type position to a type.
11601160
///
11611161
/// If the path refers to an enum variant and `permit_variants` holds,
1162-
/// the returned type is simply the provided self type `qself_ty`.
1162+
/// the returned type is simply the provided self type `self_ty`.
11631163
///
1164-
/// A path like `A::B::C::D` is understood as `<A::B::C>::D`. I.e.,
1165-
/// `qself_ty` / `qself` is `A::B::C` and `assoc_segment` is `D`.
1166-
/// We return the lowered type and the `DefId` for the whole path.
1167-
///
1168-
/// We only support associated type paths whose self type is a type parameter or a `Self`
1169-
/// type alias (in a trait impl) like `T::Ty` (where `T` is a ty param) or `Self::Ty`.
1170-
/// We **don't** support paths whose self type is an arbitrary type like `Struct::Ty` where
1171-
/// struct `Struct` impls an in-scope trait that defines an associated type called `Ty`.
1172-
/// For the latter case, we report ambiguity.
1173-
/// While desirable to support, the implementation would be non-trivial. Tracked in [#22519].
1174-
///
1175-
/// At the time of writing, *inherent associated types* are also resolved here. This however
1176-
/// is [problematic][iat]. A proper implementation would be as non-trivial as the one
1177-
/// described in the previous paragraph and their modeling of projections would likely be
1178-
/// very similar in nature.
1179-
///
1180-
/// [#22519]: https://github.com/rust-lang/rust/issues/22519
1181-
/// [iat]: https://github.com/rust-lang/rust/issues/8995#issuecomment-1569208403
1182-
// FIXME(fmease): Update docs
1183-
//
11841164
// NOTE: When this function starts resolving `Trait::AssocTy` successfully
11851165
// it should also start reporting the `BARE_TRAIT_OBJECTS` lint.
11861166
#[instrument(level = "debug", skip_all, ret)]
@@ -1192,7 +1172,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11921172
qpath_hir_id: HirId,
11931173
span: Span,
11941174
permit_variants: PermitVariants,
1195-
) -> Result<(Ty<'tcx>, DefKind, DefId), ErrorGuaranteed> {
1175+
) -> Result<(Ty<'tcx>, DefId), ErrorGuaranteed> {
11961176
let tcx = self.tcx();
11971177
match self.lower_type_relative_path(
11981178
self_ty,
@@ -1203,15 +1183,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12031183
LowerTypeRelativePathMode::Type(permit_variants),
12041184
)? {
12051185
TypeRelativePath::AssocItem(def_id, args) => {
1206-
let kind = tcx.def_kind(def_id);
1207-
self.record_res(qpath_hir_id, Ok((kind, def_id)));
12081186
let alias_ty = ty::AliasTy::new_from_args(tcx, def_id, args);
12091187
let ty = Ty::new_alias(tcx, alias_ty.kind(tcx), alias_ty);
1210-
Ok((ty, kind, def_id))
1211-
}
1212-
TypeRelativePath::Variant { adt, variant_did } => {
1213-
Ok((adt, DefKind::Variant, variant_did))
1188+
Ok((ty, def_id))
12141189
}
1190+
TypeRelativePath::Variant { adt, variant_did } => Ok((adt, variant_did)),
12151191
}
12161192
}
12171193

@@ -1255,6 +1231,27 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12551231
}
12561232

12571233
/// Lower a [type-relative][hir::QPath::TypeRelative] (and type-level) path.
1234+
///
1235+
/// A path like `A::B::C::D` is understood as `<A::B::C>::D`. I.e., the self type is `A::B::C`
1236+
/// and the `segment` is `D`.
1237+
///
1238+
/// <!-- FIXME(fmease): No longer accurate -->
1239+
///
1240+
/// We only support associated item paths whose self type is a type parameter or a `Self`
1241+
/// type alias (in a trait impl) like `T::Ty` (where `T` is a ty param) or `Self::Ty`.
1242+
/// We **don't** support paths whose self type is an arbitrary type like `Struct::Ty` where
1243+
/// struct `Struct` impls an in-scope trait that defines an associated type called `Ty`.
1244+
/// For the latter case, we report ambiguity.
1245+
/// While desirable to support, the implementation would be non-trivial. Tracked in [#22519].
1246+
///
1247+
/// <!-- FIXME(fmease): Slightly outdated, too -->
1248+
///
1249+
/// At the time of writing, *inherent associated types* are also resolved here. This however
1250+
/// is problematic. A proper implementation would be as non-trivial as the one
1251+
/// described in the previous paragraph and their modeling of projections would likely be
1252+
/// very similar in nature.
1253+
///
1254+
/// [#22519]: https://github.com/rust-lang/rust/issues/22519
12581255
#[instrument(level = "debug", skip_all, ret)]
12591256
fn lower_type_relative_path(
12601257
&self,
@@ -1287,6 +1284,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12871284
adt_def,
12881285
},
12891286
);
1287+
self.record_res(qpath_hir_id, variant_def.def_id);
12901288
return Ok(TypeRelativePath::Variant {
12911289
adt: self_ty,
12921290
variant_did: variant_def.def_id,
@@ -1298,15 +1296,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12981296
}
12991297

13001298
// FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
1301-
if let Some((did, args)) = self.probe_inherent_assoc_item(
1299+
if let Some((def_id, args)) = self.probe_inherent_assoc_item(
13021300
segment,
13031301
adt_def.did(),
13041302
self_ty,
13051303
qpath_hir_id,
13061304
span,
13071305
mode.assoc_tag(),
13081306
)? {
1309-
return Ok(TypeRelativePath::AssocItem(did, args));
1307+
self.record_res(qpath_hir_id, def_id);
1308+
return Ok(TypeRelativePath::AssocItem(def_id, args));
13101309
}
13111310
}
13121311

@@ -1445,6 +1444,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14451444
.probe_assoc_item(segment.ident, assoc_tag, qpath_hir_id, span, bound.def_id())
14461445
.expect("failed to find associated item");
14471446

1447+
self.record_res(qpath_hir_id, assoc_item.def_id);
1448+
14481449
Ok((assoc_item.def_id, bound))
14491450
}
14501451

@@ -2546,7 +2547,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25462547
hir_ty.span,
25472548
PermitVariants::No,
25482549
)
2549-
.map(|(ty, _, _)| ty)
2550+
.map(|(ty, _)| ty)
25502551
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))
25512552
}
25522553
&hir::TyKind::Path(hir::QPath::LangItem(lang_item, span)) => {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,12 +2211,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22112211
path_span,
22122212
PermitVariants::Yes,
22132213
);
2214-
let ty = result
2215-
.map(|(ty, _, _)| ty)
2216-
.unwrap_or_else(|guar| Ty::new_error(self.tcx(), guar));
2214+
let ty =
2215+
result.map(|(ty, _)| ty).unwrap_or_else(|guar| Ty::new_error(self.tcx(), guar));
22172216
let ty = LoweredTy::from_raw(self, path_span, ty);
22182217

2219-
(result.map_or(Res::Err, |(_, kind, def_id)| Res::Def(kind, def_id)), ty)
2218+
(
2219+
result.map_or(Res::Err, |(_, def_id)| {
2220+
Res::Def(self.tcx().def_kind(def_id), def_id)
2221+
}),
2222+
ty,
2223+
)
22202224
}
22212225
QPath::LangItem(lang_item, span) => {
22222226
let (res, ty) = self.resolve_lang_item_path(lang_item, span, hir_id);

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir_analysis::hir_ty_lowering::{
1717
};
1818
use rustc_infer::infer::{self, RegionVariableOrigin};
1919
use rustc_infer::traits::{DynCompatibilityViolation, Obligation};
20-
use rustc_middle::ty::typeck_results::{HasTypeDependentDefs, TypeDependentDef};
20+
use rustc_middle::ty::typeck_results::HasTypeDependentDefs;
2121
use rustc_middle::ty::{self, Const, Ty, TyCtxt, TypeVisitableExt};
2222
use rustc_session::Session;
2323
use rustc_span::{self, DUMMY_SP, ErrorGuaranteed, Ident, Span, sym};
@@ -439,8 +439,8 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
439439
self.write_ty(hir_id, ty)
440440
}
441441

442-
fn record_res(&self, hir_id: HirId, result: TypeDependentDef) {
443-
self.write_resolution(hir_id, result);
442+
fn record_res(&self, hir_id: HirId, def_id: DefId) {
443+
self.write_resolution(hir_id, Ok((self.tcx.def_kind(def_id), def_id)));
444444
}
445445

446446
fn infcx(&self) -> Option<&infer::InferCtxt<'tcx>> {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// FIXME(fmease): Description.
2+
//@ check-pass
3+
4+
#![feature(return_type_notation)]
5+
6+
trait TraitA { type Assoc: TraitB; }
7+
trait TraitB { fn assoc() -> impl Sized; }
8+
9+
fn scope<T: TraitA>()
10+
where
11+
T::Assoc::assoc(..): Iterator<Item = u8>,
12+
{
13+
let _: Vec<u8> = T::Assoc::assoc().collect();
14+
}
15+
16+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// FIXME(fmease): Description.
2+
//@ check-pass
3+
4+
#![feature(min_generic_const_args)]
5+
#![expect(incomplete_features)]
6+
7+
trait TraitA { type Assoc: TraitB; }
8+
trait TraitB { #[type_const] const ASSOC: usize; }
9+
10+
fn scope<T: TraitA>() -> [u8; T::Assoc::ASSOC] {
11+
[0; T::Assoc::ASSOC]
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)