Skip to content

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

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
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(

Diff for: compiler/rustc_hir/src/def.rs

+6-4
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

Diff for: compiler/rustc_hir/src/target.rs

+1-1
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,

Diff for: compiler/rustc_hir_analysis/src/astconv/mod.rs

+10-8
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,

Diff for: compiler/rustc_hir_analysis/src/check/check.rs

+1-1
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);

Diff for: compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+2-2
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.

Diff for: compiler/rustc_hir_analysis/src/variance/constraints.rs

+4-5
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;

Diff for: compiler/rustc_hir_analysis/src/variance/mod.rs

+2-3
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(());

Diff for: compiler/rustc_hir_analysis/src/variance/terms.rs

+2-3
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
}

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+4-1
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() => {

Diff for: compiler/rustc_hir_typeck/src/mem_categorization.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,10 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
557557
Ok(adt_def.variant_index_with_ctor_id(variant_ctor_id))
558558
}
559559
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), _)
560-
| Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
560+
| Res::Def(
561+
DefKind::Struct | DefKind::Union | DefKind::TyAlias { .. } | DefKind::AssocTy,
562+
_,
563+
)
561564
| Res::SelfCtor(..)
562565
| Res::SelfTyParam { .. }
563566
| Res::SelfTyAlias { .. } => {

Diff for: compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
951951
//
952952
// See the `need_type_info/issue-103053.rs` test for
953953
// a example.
954-
if !matches!(path.res, Res::Def(DefKind::TyAlias, _)) => {
954+
if !matches!(path.res, Res::Def(DefKind::TyAlias { .. }, _)) => {
955955
if let Some(ty) = self.opt_node_type(expr.hir_id)
956956
&& let ty::Adt(_, args) = ty.kind()
957957
{
@@ -1080,7 +1080,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10801080
) => {
10811081
if tcx.res_generics_def_id(path.res) != Some(def.did()) {
10821082
match path.res {
1083-
Res::Def(DefKind::TyAlias, _) => {
1083+
Res::Def(DefKind::TyAlias { .. }, _) => {
10841084
// FIXME: Ideally we should support this. For that
10851085
// we have to map back from the self type to the
10861086
// type alias though. That's difficult.

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
819819
| DefKind::Enum
820820
| DefKind::Variant
821821
| DefKind::Trait
822-
| DefKind::TyAlias
822+
| DefKind::TyAlias { .. }
823823
| DefKind::ForeignTy
824824
| DefKind::TraitAlias
825825
| DefKind::AssocTy
@@ -854,7 +854,7 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
854854
| DefKind::Enum
855855
| DefKind::Variant
856856
| DefKind::Trait
857-
| DefKind::TyAlias
857+
| DefKind::TyAlias { .. }
858858
| DefKind::ForeignTy
859859
| DefKind::TraitAlias
860860
| DefKind::AssocTy
@@ -895,7 +895,7 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
895895
| DefKind::Variant
896896
| DefKind::Trait
897897
| DefKind::Impl { .. } => true,
898-
DefKind::TyAlias
898+
DefKind::TyAlias { .. }
899899
| DefKind::ForeignTy
900900
| DefKind::TraitAlias
901901
| DefKind::AssocTy
@@ -930,7 +930,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
930930
| DefKind::Enum
931931
| DefKind::Variant
932932
| DefKind::Trait
933-
| DefKind::TyAlias
933+
| DefKind::TyAlias { .. }
934934
| DefKind::ForeignTy
935935
| DefKind::TraitAlias
936936
| DefKind::AssocTy
@@ -974,7 +974,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
974974
| DefKind::Const
975975
| DefKind::Fn
976976
| DefKind::ForeignMod
977-
| DefKind::TyAlias
977+
| DefKind::TyAlias { .. }
978978
| DefKind::OpaqueTy
979979
| DefKind::Enum
980980
| DefKind::Union
@@ -1067,9 +1067,8 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
10671067
| DefKind::Closure
10681068
| DefKind::Generator
10691069
| DefKind::ExternCrate => false,
1070-
DefKind::TyAlias => {
1071-
tcx.features().lazy_type_alias
1072-
|| tcx.type_of(def_id).instantiate_identity().has_opaque_types()
1070+
DefKind::TyAlias { lazy } => {
1071+
lazy || tcx.type_of(def_id).instantiate_identity().has_opaque_types()
10731072
}
10741073
}
10751074
}
@@ -1081,7 +1080,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
10811080
| DefKind::Enum
10821081
| DefKind::Variant
10831082
| DefKind::Trait
1084-
| DefKind::TyAlias
1083+
| DefKind::TyAlias { .. }
10851084
| DefKind::ForeignTy
10861085
| DefKind::TraitAlias
10871086
| DefKind::AssocTy
@@ -1121,7 +1120,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11211120
| DefKind::Fn
11221121
| DefKind::Const
11231122
| DefKind::Static(..)
1124-
| DefKind::TyAlias
1123+
| DefKind::TyAlias { .. }
11251124
| DefKind::ForeignTy
11261125
| DefKind::Impl { .. }
11271126
| DefKind::AssocFn
@@ -1181,7 +1180,7 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
11811180
| DefKind::Const
11821181
| DefKind::Static(..)
11831182
| DefKind::Ctor(..)
1184-
| DefKind::TyAlias
1183+
| DefKind::TyAlias { .. }
11851184
| DefKind::OpaqueTy
11861185
| DefKind::ForeignTy
11871186
| DefKind::Impl { .. }
@@ -1222,7 +1221,7 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
12221221
| DefKind::AssocConst
12231222
| DefKind::AnonConst
12241223
| DefKind::Static(..)
1225-
| DefKind::TyAlias
1224+
| DefKind::TyAlias { .. }
12261225
| DefKind::OpaqueTy
12271226
| DefKind::Impl { of_trait: false }
12281227
| DefKind::ForeignTy
@@ -1255,7 +1254,7 @@ fn should_encode_const(def_kind: DefKind) -> bool {
12551254
| DefKind::Field
12561255
| DefKind::Fn
12571256
| DefKind::Static(..)
1258-
| DefKind::TyAlias
1257+
| DefKind::TyAlias { .. }
12591258
| DefKind::OpaqueTy
12601259
| DefKind::ForeignTy
12611260
| DefKind::Impl { .. }

Diff for: compiler/rustc_metadata/src/rmeta/table.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ fixed_size_enum! {
126126
( Enum )
127127
( Variant )
128128
( Trait )
129-
( TyAlias )
129+
( TyAlias { lazy: false } )
130+
( TyAlias { lazy: true } )
130131
( ForeignTy )
131132
( TraitAlias )
132133
( AssocTy )

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ impl<'hir> Map<'hir> {
196196
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
197197
ItemKind::Mod(..) => DefKind::Mod,
198198
ItemKind::OpaqueTy(..) => DefKind::OpaqueTy,
199-
ItemKind::TyAlias(..) => DefKind::TyAlias,
199+
ItemKind::TyAlias(..) => {
200+
DefKind::TyAlias { lazy: self.tcx.features().lazy_type_alias }
201+
}
200202
ItemKind::Enum(..) => DefKind::Enum,
201203
ItemKind::Struct(..) => DefKind::Struct,
202204
ItemKind::Union(..) => DefKind::Union,

Diff for: compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ rustc_queries! {
231231
action = {
232232
use rustc_hir::def::DefKind;
233233
match tcx.def_kind(key) {
234-
DefKind::TyAlias => "expanding type alias",
234+
DefKind::TyAlias { .. } => "expanding type alias",
235235
DefKind::TraitAlias => "expanding trait alias",
236236
_ => "computing type of",
237237
}

Diff for: compiler/rustc_middle/src/ty/adt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'tcx> AdtDef<'tcx> {
448448
Res::Def(DefKind::Ctor(..), cid) => self.variant_with_ctor_id(cid),
449449
Res::Def(DefKind::Struct, _)
450450
| Res::Def(DefKind::Union, _)
451-
| Res::Def(DefKind::TyAlias, _)
451+
| Res::Def(DefKind::TyAlias { .. }, _)
452452
| Res::Def(DefKind::AssocTy, _)
453453
| Res::SelfTyParam { .. }
454454
| Res::SelfTyAlias { .. }

Diff for: compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ impl<'tcx> TyCtxt<'tcx> {
10621062
if let Some(hir::FnDecl { output: hir::FnRetTy::Return(hir_output), .. }) = self.hir().fn_decl_by_hir_id(hir_id)
10631063
&& let hir::TyKind::Path(hir::QPath::Resolved(
10641064
None,
1065-
hir::Path { res: hir::def::Res::Def(DefKind::TyAlias, def_id), .. }, )) = hir_output.kind
1065+
hir::Path { res: hir::def::Res::Def(DefKind::TyAlias { .. }, def_id), .. }, )) = hir_output.kind
10661066
&& let Some(local_id) = def_id.as_local()
10671067
&& let Some(alias_ty) = self.hir().get_by_def_id(local_id).alias_ty() // it is type alias
10681068
&& let Some(alias_generics) = self.hir().get_by_def_id(local_id).generics()

Diff for: compiler/rustc_middle/src/ty/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for IsSuggestableVisitor<'tcx> {
492492
Alias(Opaque, AliasTy { def_id, .. }) => {
493493
let parent = self.tcx.parent(def_id);
494494
let parent_ty = self.tcx.type_of(parent).instantiate_identity();
495-
if let DefKind::TyAlias | DefKind::AssocTy = self.tcx.def_kind(parent)
495+
if let DefKind::TyAlias { .. } | DefKind::AssocTy = self.tcx.def_kind(parent)
496496
&& let Alias(Opaque, AliasTy { def_id: parent_opaque_def_id, .. }) = *parent_ty.kind()
497497
&& parent_opaque_def_id == def_id
498498
{
@@ -576,7 +576,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for MakeSuggestableFolder<'tcx> {
576576
Alias(Opaque, AliasTy { def_id, .. }) => {
577577
let parent = self.tcx.parent(def_id);
578578
let parent_ty = self.tcx.type_of(parent).instantiate_identity();
579-
if let hir::def::DefKind::TyAlias | hir::def::DefKind::AssocTy = self.tcx.def_kind(parent)
579+
if let hir::def::DefKind::TyAlias { .. } | hir::def::DefKind::AssocTy = self.tcx.def_kind(parent)
580580
&& let Alias(Opaque, AliasTy { def_id: parent_opaque_def_id, .. }) = *parent_ty.kind()
581581
&& parent_opaque_def_id == def_id
582582
{

0 commit comments

Comments
 (0)