Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3cd0a10

Browse files
authored
Rollup merge of rust-lang#114566 - fmease:type-alias-laziness-is-crate-specific, r=oli-obk
Store the laziness of type aliases in their `DefKind` Previously, we would treat paths referring to type aliases as *lazy* type aliases if the current crate had lazy type aliases enabled independently of whether the crate which the alias was defined in had the feature enabled or not. With this PR, the laziness of a type alias depends on the crate it is defined in. This generally makes more sense to me especially if / once lazy type aliases become the default in a new edition and we need to think about *edition interoperability*: Consider the hypothetical case where the dependency crate has an older edition (and thus eager type aliases), it exports a type alias with bounds & a where-clause (which are void but technically valid), the dependent crate has the latest edition (and thus lazy type aliases) and it uses that type alias. Arguably, the bounds should *not* be checked since at any time, the dependency crate should be allowed to change the bounds at will with a *non*-major version bump & without negatively affecting downstream crates. As for the reverse case (dependency: lazy type aliases, dependent: eager type aliases), I guess it rules out anything from slight confusion to mild annoyance from upstream crate authors that would be caused by the compiler ignoring the bounds of their type aliases in downstream crates with older editions. --- This fixes rust-lang#114468 since before, my assumption that the type alias associated with a given weak projection was lazy (and therefore had its variances computed) did not necessarily hold in cross-crate scenarios (which [I kinda had a hunch about](rust-lang#114253 (comment))) as outlined above. Now it does hold. `@rustbot` label F-lazy_type_alias r? `@oli-obk`
2 parents 28ee1a9 + 5468336 commit 3cd0a10

File tree

49 files changed

+208
-93
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+208
-93
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
516516
// be the same as those of the ADT.
517517
// FIXME: We should be able to do something similar to
518518
// match_adt_and_segment in this case.
519-
Res::Def(DefKind::TyAlias, _) => (),
519+
Res::Def(DefKind::TyAlias { .. }, _) => (),
520520
_ => {
521521
if let Some(last_segment) = path.segments.last() {
522522
if let Some(highlight) = self.match_adt_and_segment(

compiler/rustc_hir/src/def.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ pub enum DefKind {
6161
Variant,
6262
Trait,
6363
/// Type alias: `type Foo = Bar;`
64-
TyAlias,
64+
TyAlias {
65+
lazy: bool,
66+
},
6567
/// Type from an `extern` block.
6668
ForeignTy,
6769
/// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
@@ -141,7 +143,7 @@ impl DefKind {
141143
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
142144
DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
143145
DefKind::OpaqueTy => "opaque type",
144-
DefKind::TyAlias => "type alias",
146+
DefKind::TyAlias { .. } => "type alias",
145147
DefKind::TraitAlias => "trait alias",
146148
DefKind::AssocTy => "associated type",
147149
DefKind::Union => "union",
@@ -197,7 +199,7 @@ impl DefKind {
197199
| DefKind::Variant
198200
| DefKind::Trait
199201
| DefKind::OpaqueTy
200-
| DefKind::TyAlias
202+
| DefKind::TyAlias { .. }
201203
| DefKind::ForeignTy
202204
| DefKind::TraitAlias
203205
| DefKind::AssocTy
@@ -248,7 +250,7 @@ impl DefKind {
248250
| DefKind::Enum
249251
| DefKind::Variant
250252
| DefKind::Trait
251-
| DefKind::TyAlias
253+
| DefKind::TyAlias { .. }
252254
| DefKind::ForeignTy
253255
| DefKind::TraitAlias
254256
| DefKind::AssocTy

compiler/rustc_hir/src/target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Target {
101101
DefKind::Mod => Target::Mod,
102102
DefKind::ForeignMod => Target::ForeignMod,
103103
DefKind::GlobalAsm => Target::GlobalAsm,
104-
DefKind::TyAlias => Target::TyAlias,
104+
DefKind::TyAlias { .. } => Target::TyAlias,
105105
DefKind::OpaqueTy => Target::OpaqueTy,
106106
DefKind::Enum => Target::Enum,
107107
DefKind::Struct => Target::Struct,

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -907,19 +907,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
907907
did: DefId,
908908
item_segment: &hir::PathSegment<'_>,
909909
) -> Ty<'tcx> {
910+
let tcx = self.tcx();
910911
let args = self.ast_path_args_for_ty(span, did, item_segment);
911-
let ty = self.tcx().at(span).type_of(did);
912+
let ty = tcx.at(span).type_of(did);
912913

913-
if matches!(self.tcx().def_kind(did), DefKind::TyAlias)
914-
&& (ty.skip_binder().has_opaque_types() || self.tcx().features().lazy_type_alias)
914+
if let DefKind::TyAlias { lazy } = tcx.def_kind(did)
915+
&& (lazy || ty.skip_binder().has_opaque_types())
915916
{
916917
// Type aliases referring to types that contain opaque types (but aren't just directly
917-
// referencing a single opaque type) get encoded as a type alias that normalization will
918+
// referencing a single opaque type) as well as those defined in crates that have the
919+
// feature `lazy_type_alias` enabled get encoded as a type alias that normalization will
918920
// then actually instantiate the where bounds of.
919-
let alias_ty = self.tcx().mk_alias_ty(did, args);
920-
Ty::new_alias(self.tcx(), ty::Weak, alias_ty)
921+
let alias_ty = tcx.mk_alias_ty(did, args);
922+
Ty::new_alias(tcx, ty::Weak, alias_ty)
921923
} else {
922-
ty.instantiate(self.tcx(), args)
924+
ty.instantiate(tcx, args)
923925
}
924926
}
925927

@@ -2158,7 +2160,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21582160
}
21592161
Res::Def(
21602162
DefKind::Enum
2161-
| DefKind::TyAlias
2163+
| DefKind::TyAlias { .. }
21622164
| DefKind::Struct
21632165
| DefKind::Union
21642166
| DefKind::ForeignTy,

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
728728
check_opaque(tcx, id);
729729
}
730730
}
731-
DefKind::TyAlias => {
731+
DefKind::TyAlias { .. } => {
732732
let pty_ty = tcx.type_of(id.owner_id).instantiate_identity();
733733
let generics = tcx.generics_of(id.owner_id);
734734
check_type_params_are_used(tcx, &generics, pty_ty);

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
14801480
DefKind::Struct
14811481
| DefKind::Union
14821482
| DefKind::Enum
1483-
| DefKind::TyAlias
1483+
| DefKind::TyAlias { .. }
14841484
| DefKind::Trait,
14851485
def_id,
14861486
) if depth == 0 => Some(def_id),
@@ -1990,7 +1990,7 @@ fn is_late_bound_map(
19901990

19911991
hir::TyKind::Path(hir::QPath::Resolved(
19921992
None,
1993-
hir::Path { res: Res::Def(DefKind::TyAlias, alias_def), segments, span },
1993+
hir::Path { res: Res::Def(DefKind::TyAlias { .. }, alias_def), segments, span },
19941994
)) => {
19951995
// See comments on `ConstrainedCollectorPostAstConv` for why this arm does not just consider
19961996
// args to be unconstrained.

compiler/rustc_hir_analysis/src/variance/constraints.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
7878
}
7979
}
8080
DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id),
81-
DefKind::TyAlias
82-
if tcx.features().lazy_type_alias
83-
|| tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
81+
DefKind::TyAlias { lazy }
82+
if lazy || tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
8483
{
8584
constraint_cx.build_constraints_for_item(def_id)
8685
}
@@ -111,8 +110,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
111110

112111
// The type as returned by `type_of` is the underlying type and generally not a weak projection.
113112
// Therefore we need to check the `DefKind` first.
114-
if let DefKind::TyAlias = tcx.def_kind(def_id)
115-
&& (tcx.features().lazy_type_alias || ty.has_opaque_types())
113+
if let DefKind::TyAlias { lazy } = tcx.def_kind(def_id)
114+
&& (lazy || ty.has_opaque_types())
116115
{
117116
self.add_constraints_from_ty(current_item, ty, self.covariant);
118117
return;

compiler/rustc_hir_analysis/src/variance/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
5656
let crate_map = tcx.crate_variances(());
5757
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
5858
}
59-
DefKind::TyAlias
60-
if tcx.features().lazy_type_alias
61-
|| tcx.type_of(item_def_id).instantiate_identity().has_opaque_types() =>
59+
DefKind::TyAlias { lazy }
60+
if lazy || tcx.type_of(item_def_id).instantiate_identity().has_opaque_types() =>
6261
{
6362
// These are inferred.
6463
let crate_map = tcx.crate_variances(());

compiler/rustc_hir_analysis/src/variance/terms.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
9797
}
9898
}
9999
DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id),
100-
DefKind::TyAlias
101-
if tcx.features().lazy_type_alias
102-
|| tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
100+
DefKind::TyAlias { lazy }
101+
if lazy || tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
103102
{
104103
terms_cx.add_inferreds_for_item(def_id)
105104
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13591359
}
13601360
_ => bug!("unexpected type: {:?}", ty.normalized),
13611361
},
1362-
Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
1362+
Res::Def(
1363+
DefKind::Struct | DefKind::Union | DefKind::TyAlias { .. } | DefKind::AssocTy,
1364+
_,
1365+
)
13631366
| Res::SelfTyParam { .. }
13641367
| Res::SelfTyAlias { .. } => match ty.normalized.ty_adt_def() {
13651368
Some(adt) if !adt.is_enum() => {

0 commit comments

Comments
 (0)