Skip to content

Commit 6b99cf1

Browse files
committed
Auto merge of #116163 - compiler-errors:lazyness, r=oli-obk
Don't store lazyness in `DefKind::TyAlias` 1. Don't store lazyness of a type alias in its `DefKind`, but instead via a query. 2. This allows us to treat type aliases as lazy if `#[feature(lazy_type_alias)]` *OR* if the alias contains a TAIT, rather than having checks for both in separate parts of the codebase. r? `@oli-obk` cc `@fmease`
2 parents e1636a0 + d6ce9ce commit 6b99cf1

File tree

51 files changed

+128
-189
lines changed

Some content is hidden

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

51 files changed

+128
-189
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

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ pub enum DefKind {
6161
Variant,
6262
Trait,
6363
/// Type alias: `type Foo = Bar;`
64-
TyAlias {
65-
lazy: bool,
66-
},
64+
TyAlias,
6765
/// Type from an `extern` block.
6866
ForeignTy,
6967
/// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
@@ -143,7 +141,7 @@ impl DefKind {
143141
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
144142
DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
145143
DefKind::OpaqueTy => "opaque type",
146-
DefKind::TyAlias { .. } => "type alias",
144+
DefKind::TyAlias => "type alias",
147145
DefKind::TraitAlias => "trait alias",
148146
DefKind::AssocTy => "associated type",
149147
DefKind::Union => "union",
@@ -199,7 +197,7 @@ impl DefKind {
199197
| DefKind::Variant
200198
| DefKind::Trait
201199
| DefKind::OpaqueTy
202-
| DefKind::TyAlias { .. }
200+
| DefKind::TyAlias
203201
| DefKind::ForeignTy
204202
| DefKind::TraitAlias
205203
| DefKind::AssocTy
@@ -250,7 +248,7 @@ impl DefKind {
250248
| DefKind::Enum
251249
| DefKind::Variant
252250
| DefKind::Trait
253-
| DefKind::TyAlias { .. }
251+
| DefKind::TyAlias
254252
| DefKind::ForeignTy
255253
| DefKind::TraitAlias
256254
| 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

+5-12
Original file line numberDiff line numberDiff line change
@@ -909,23 +909,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
909909
let tcx = self.tcx();
910910
let args = self.ast_path_args_for_ty(span, did, item_segment);
911911

912-
if let DefKind::TyAlias { lazy: true } = tcx.def_kind(did) {
912+
if let DefKind::TyAlias = tcx.def_kind(did)
913+
&& tcx.type_alias_is_lazy(did)
914+
{
913915
// Type aliases defined in crates that have the
914916
// feature `lazy_type_alias` enabled get encoded as a type alias that normalization will
915917
// then actually instantiate the where bounds of.
916918
let alias_ty = tcx.mk_alias_ty(did, args);
917919
Ty::new_alias(tcx, ty::Weak, alias_ty)
918920
} else {
919-
let ty = tcx.at(span).type_of(did);
920-
if ty.skip_binder().has_opaque_types() {
921-
// Type aliases referring to types that contain opaque types (but aren't just directly
922-
// referencing a single opaque type) get encoded as a type alias that normalization will
923-
// then actually instantiate the where bounds of.
924-
let alias_ty = tcx.mk_alias_ty(did, args);
925-
Ty::new_alias(tcx, ty::Weak, alias_ty)
926-
} else {
927-
ty.instantiate(tcx, args)
928-
}
921+
tcx.at(span).type_of(did).instantiate(tcx, args)
929922
}
930923
}
931924

@@ -2164,7 +2157,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21642157
}
21652158
Res::Def(
21662159
DefKind::Enum
2167-
| DefKind::TyAlias { .. }
2160+
| DefKind::TyAlias
21682161
| DefKind::Struct
21692162
| DefKind::Union
21702163
| DefKind::ForeignTy,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
640640
check_opaque(tcx, id);
641641
}
642642
}
643-
DefKind::TyAlias { .. } => {
643+
DefKind::TyAlias => {
644644
let pty_ty = tcx.type_of(id.owner_id).instantiate_identity();
645645
let generics = tcx.generics_of(id.owner_id);
646646
check_type_params_are_used(tcx, &generics, pty_ty);

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
246246
// `ForeignItem`s are handled separately.
247247
hir::ItemKind::ForeignMod { .. } => {}
248248
hir::ItemKind::TyAlias(hir_ty, ast_generics) => {
249-
if tcx.features().lazy_type_alias
250-
|| tcx.type_of(item.owner_id).skip_binder().has_opaque_types()
251-
{
249+
if tcx.type_alias_is_lazy(item.owner_id) {
252250
// Bounds of lazy type aliases and of eager ones that contain opaque types are respected.
253251
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`.
254252
check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow);
@@ -1711,10 +1709,8 @@ fn check_variances_for_type_defn<'tcx>(
17111709
}
17121710
}
17131711
ItemKind::TyAlias(..) => {
1714-
let ty = tcx.type_of(item.owner_id).instantiate_identity();
1715-
1716-
if tcx.features().lazy_type_alias || ty.has_opaque_types() {
1717-
if ty.references_error() {
1712+
if tcx.type_alias_is_lazy(item.owner_id) {
1713+
if tcx.type_of(item.owner_id).skip_binder().references_error() {
17181714
return;
17191715
}
17201716
} else {

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

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub fn provide(providers: &mut Providers) {
5858
*providers = Providers {
5959
type_of: type_of::type_of,
6060
type_of_opaque: type_of::type_of_opaque,
61+
type_alias_is_lazy: type_of::type_alias_is_lazy,
6162
item_bounds: item_bounds::item_bounds,
6263
explicit_item_bounds: item_bounds::explicit_item_bounds,
6364
generics_of: generics_of::generics_of,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15191519
DefKind::Struct
15201520
| DefKind::Union
15211521
| DefKind::Enum
1522-
| DefKind::TyAlias { .. }
1522+
| DefKind::TyAlias
15231523
| DefKind::Trait,
15241524
def_id,
15251525
) if depth == 0 => Some(def_id),
@@ -2029,7 +2029,7 @@ fn is_late_bound_map(
20292029

20302030
hir::TyKind::Path(hir::QPath::Resolved(
20312031
None,
2032-
hir::Path { res: Res::Def(DefKind::TyAlias { .. }, alias_def), segments, span },
2032+
hir::Path { res: Res::Def(DefKind::TyAlias, alias_def), segments, span },
20332033
)) => {
20342034
// See comments on `ConstrainedCollectorPostAstConv` for why this arm does not just consider
20352035
// args to be unconstrained.

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

+22
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,25 @@ fn check_feature_inherent_assoc_ty(tcx: TyCtxt<'_>, span: Span) {
623623
.emit();
624624
}
625625
}
626+
627+
pub fn type_alias_is_lazy<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
628+
use hir::intravisit::Visitor;
629+
if tcx.features().lazy_type_alias {
630+
return true;
631+
}
632+
struct HasTait {
633+
has_type_alias_impl_trait: bool,
634+
}
635+
impl<'tcx> Visitor<'tcx> for HasTait {
636+
fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
637+
if let hir::TyKind::OpaqueDef(..) = t.kind {
638+
self.has_type_alias_impl_trait = true;
639+
} else {
640+
hir::intravisit::walk_ty(self, t);
641+
}
642+
}
643+
}
644+
let mut has_tait = HasTait { has_type_alias_impl_trait: false };
645+
has_tait.visit_ty(tcx.hir().expect_item(def_id).expect_ty_alias().0);
646+
has_tait.has_type_alias_impl_trait
647+
}

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use hir::def_id::{DefId, LocalDefId};
77
use rustc_hir as hir;
88
use rustc_hir::def::DefKind;
9-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
9+
use rustc_middle::ty::{self, Ty, TyCtxt};
1010
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
1111

1212
use super::terms::VarianceTerm::*;
@@ -78,9 +78,7 @@ 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 { lazy }
82-
if lazy || tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
83-
{
81+
DefKind::TyAlias if tcx.type_alias_is_lazy(def_id) => {
8482
constraint_cx.build_constraints_for_item(def_id)
8583
}
8684
_ => {}
@@ -110,8 +108,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
110108

111109
// The type as returned by `type_of` is the underlying type and generally not a weak projection.
112110
// Therefore we need to check the `DefKind` first.
113-
if let DefKind::TyAlias { lazy } = tcx.def_kind(def_id)
114-
&& (lazy || ty.has_opaque_types())
111+
if let DefKind::TyAlias = tcx.def_kind(def_id)
112+
&& tcx.type_alias_is_lazy(def_id)
115113
{
116114
self.add_constraints_from_ty(current_item, ty, self.covariant);
117115
return;

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_middle::query::Providers;
1010
use rustc_middle::ty::{self, CrateVariancesMap, GenericArgsRef, Ty, TyCtxt};
11-
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt};
11+
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable};
1212
use std::ops::ControlFlow;
1313

1414
/// Defines the `TermsContext` basically houses an arena where we can
@@ -56,9 +56,7 @@ 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 { lazy }
60-
if lazy || tcx.type_of(item_def_id).instantiate_identity().has_opaque_types() =>
61-
{
59+
DefKind::TyAlias if tcx.type_alias_is_lazy(item_def_id) => {
6260
// These are inferred.
6361
let crate_map = tcx.crate_variances(());
6462
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use rustc_arena::DroplessArena;
1313
use rustc_hir::def::DefKind;
1414
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
15-
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
15+
use rustc_middle::ty::{self, TyCtxt};
1616
use std::fmt;
1717

1818
use self::VarianceTerm::*;
@@ -97,9 +97,7 @@ 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 { lazy }
101-
if lazy || tcx.type_of(def_id).instantiate_identity().has_opaque_types() =>
102-
{
100+
DefKind::TyAlias if tcx.type_alias_is_lazy(def_id) => {
103101
terms_cx.add_inferreds_for_item(def_id)
104102
}
105103
_ => {}

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -1370,10 +1370,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13701370
}
13711371
_ => bug!("unexpected type: {:?}", ty.normalized),
13721372
},
1373-
Res::Def(
1374-
DefKind::Struct | DefKind::Union | DefKind::TyAlias { .. } | DefKind::AssocTy,
1375-
_,
1376-
)
1373+
Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
13771374
| Res::SelfTyParam { .. }
13781375
| Res::SelfTyAlias { .. } => match ty.normalized.ty_adt_def() {
13791376
Some(adt) if !adt.is_enum() => {

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,7 @@ 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(
561-
DefKind::Struct | DefKind::Union | DefKind::TyAlias { .. } | DefKind::AssocTy,
562-
_,
563-
)
560+
| Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
564561
| Res::SelfCtor(..)
565562
| Res::SelfTyParam { .. }
566563
| 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
@@ -918,7 +918,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
918918
//
919919
// See the `need_type_info/issue-103053.rs` test for
920920
// a example.
921-
if !matches!(path.res, Res::Def(DefKind::TyAlias { .. }, _)) => {
921+
if !matches!(path.res, Res::Def(DefKind::TyAlias, _)) => {
922922
if let Some(ty) = self.opt_node_type(expr.hir_id)
923923
&& let ty::Adt(_, args) = ty.kind()
924924
{
@@ -1047,7 +1047,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10471047
) => {
10481048
if tcx.res_generics_def_id(path.res) != Some(def.did()) {
10491049
match path.res {
1050-
Res::Def(DefKind::TyAlias { .. }, _) => {
1050+
Res::Def(DefKind::TyAlias, _) => {
10511051
// FIXME: Ideally we should support this. For that
10521052
// we have to map back from the self type to the
10531053
// type alias though. That's difficult.

Diff for: compiler/rustc_lint/src/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1454,13 +1454,13 @@ impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
14541454
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
14551455
let hir::ItemKind::TyAlias(hir_ty, type_alias_generics) = &item.kind else { return };
14561456

1457-
if cx.tcx.features().lazy_type_alias {
1458-
// Bounds of lazy type aliases are respected.
1457+
// Bounds of lazy type aliases and TAITs are respected.
1458+
if cx.tcx.type_alias_is_lazy(item.owner_id) {
14591459
return;
14601460
}
14611461

14621462
let ty = cx.tcx.type_of(item.owner_id).skip_binder();
1463-
if ty.has_opaque_types() || ty.has_inherent_projections() {
1463+
if ty.has_inherent_projections() {
14641464
// Bounds of type aliases that contain opaque types or inherent projections are respected.
14651465
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`, `type X = Type::Inherent;`.
14661466
return;

Diff for: compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ provide! { tcx, def_id, other, cdata,
210210
inferred_outlives_of => { table_defaulted_array }
211211
super_predicates_of => { table }
212212
type_of => { table }
213+
type_alias_is_lazy => { cdata.root.tables.type_alias_is_lazy.get(cdata, def_id.index) }
213214
variances_of => { table }
214215
fn_sig => { table }
215216
codegen_fn_attrs => { table }

0 commit comments

Comments
 (0)