Skip to content

Commit a728895

Browse files
committed
yikes
1 parent 51efd8f commit a728895

File tree

22 files changed

+366
-246
lines changed

22 files changed

+366
-246
lines changed

compiler/rustc_macros/src/lib.rs

+4-21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod diagnostics;
1717
mod extension;
1818
mod hash_stable;
1919
mod lift;
20+
mod noop_type_traversable;
2021
mod query;
2122
mod serialize;
2223
mod symbols;
@@ -81,27 +82,9 @@ decl_derive!([TyDecodable] => serialize::type_decodable_derive);
8182
decl_derive!([TyEncodable] => serialize::type_encodable_derive);
8283
decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
8384
decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
84-
decl_derive!(
85-
[TypeFoldable, attributes(type_foldable)] =>
86-
/// Derives `TypeFoldable` for the annotated `struct` or `enum` (`union` is not supported).
87-
///
88-
/// The fold will produce a value of the same struct or enum variant as the input, with
89-
/// each field respectively folded using the `TypeFoldable` implementation for its type.
90-
/// However, if a field of a struct or an enum variant is annotated with
91-
/// `#[type_foldable(identity)]` then that field will retain its incumbent value (and its
92-
/// type is not required to implement `TypeFoldable`).
93-
type_foldable::type_foldable_derive
94-
);
95-
decl_derive!(
96-
[TypeVisitable, attributes(type_visitable)] =>
97-
/// Derives `TypeVisitable` for the annotated `struct` or `enum` (`union` is not supported).
98-
///
99-
/// Each field of the struct or enum variant will be visited in definition order, using the
100-
/// `TypeVisitable` implementation for its type. However, if a field of a struct or an enum
101-
/// variant is annotated with `#[type_visitable(ignore)]` then that field will not be
102-
/// visited (and its type is not required to implement `TypeVisitable`).
103-
type_visitable::type_visitable_derive
104-
);
85+
decl_derive!([NoopTypeTraversable] => noop_type_traversable::noop_type_traversable_derive);
86+
decl_derive!([TypeVisitable] => type_visitable::type_visitable_derive);
87+
decl_derive!([TypeFoldable] => type_foldable::type_foldable_derive);
10588
decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
10689
decl_derive!(
10790
[Diagnostic, attributes(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use quote::quote;
2+
use syn::parse_quote;
3+
4+
pub(super) fn noop_type_traversable_derive(
5+
mut s: synstructure::Structure<'_>,
6+
) -> proc_macro2::TokenStream {
7+
if let syn::Data::Union(_) = s.ast().data {
8+
panic!("cannot derive on union")
9+
}
10+
11+
s.underscore_const(true);
12+
13+
if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
14+
s.add_impl_generic(parse_quote! { 'tcx });
15+
}
16+
17+
s.add_bounds(synstructure::AddBounds::None);
18+
let mut where_clauses = None;
19+
s.add_trait_bounds(
20+
&parse_quote!(
21+
::rustc_middle::ty::traverse::TypeTraversable<
22+
::rustc_middle::ty::TyCtxt<'tcx>,
23+
Kind = ::rustc_middle::ty::traverse::NoopTypeTraversal,
24+
>
25+
),
26+
&mut where_clauses,
27+
synstructure::AddBounds::Fields,
28+
);
29+
for pred in where_clauses.into_iter().flat_map(|c| c.predicates) {
30+
s.add_where_predicate(pred);
31+
}
32+
33+
s.bound_impl(
34+
quote!(::rustc_middle::ty::traverse::TypeTraversable<::rustc_middle::ty::TyCtxt<'tcx>>),
35+
quote! {
36+
type Kind = ::rustc_middle::ty::traverse::NoopTypeTraversal;
37+
},
38+
)
39+
}

compiler/rustc_macros/src/type_foldable.rs

+14-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use quote::{ToTokens, quote};
1+
use quote::quote;
22
use syn::parse_quote;
33

44
pub(super) fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
@@ -12,34 +12,24 @@ pub(super) fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_m
1212
s.add_impl_generic(parse_quote! { 'tcx });
1313
}
1414

15-
s.add_bounds(synstructure::AddBounds::Generics);
15+
s.add_bounds(synstructure::AddBounds::None);
16+
let mut where_clauses = None;
17+
s.add_trait_bounds(
18+
&parse_quote!(::rustc_type_ir::traverse::OptTryFoldWith<::rustc_middle::ty::TyCtxt<'tcx>>),
19+
&mut where_clauses,
20+
synstructure::AddBounds::Fields,
21+
);
22+
for pred in where_clauses.into_iter().flat_map(|c| c.predicates) {
23+
s.add_where_predicate(pred);
24+
}
25+
1626
s.bind_with(|_| synstructure::BindStyle::Move);
1727
let body_fold = s.each_variant(|vi| {
1828
let bindings = vi.bindings();
1929
vi.construct(|_, index| {
2030
let bind = &bindings[index];
21-
22-
let mut fixed = false;
23-
24-
// retain value of fields with #[type_foldable(identity)]
25-
bind.ast().attrs.iter().for_each(|x| {
26-
if !x.path().is_ident("type_foldable") {
27-
return;
28-
}
29-
let _ = x.parse_nested_meta(|nested| {
30-
if nested.path.is_ident("identity") {
31-
fixed = true;
32-
}
33-
Ok(())
34-
});
35-
});
36-
37-
if fixed {
38-
bind.to_token_stream()
39-
} else {
40-
quote! {
41-
::rustc_middle::ty::fold::TypeFoldable::try_fold_with(#bind, __folder)?
42-
}
31+
quote! {
32+
::rustc_middle::ty::traverse::OptTryFoldWith::mk_try_fold_with()(#bind, __folder)?
4333
}
4434
})
4535
});

compiler/rustc_macros/src/type_visitable.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,27 @@ pub(super) fn type_visitable_derive(
1010

1111
s.underscore_const(true);
1212

13-
// ignore fields with #[type_visitable(ignore)]
14-
s.filter(|bi| {
15-
let mut ignored = false;
16-
17-
bi.ast().attrs.iter().for_each(|attr| {
18-
if !attr.path().is_ident("type_visitable") {
19-
return;
20-
}
21-
let _ = attr.parse_nested_meta(|nested| {
22-
if nested.path.is_ident("ignore") {
23-
ignored = true;
24-
}
25-
Ok(())
26-
});
27-
});
28-
29-
!ignored
30-
});
31-
3213
if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
3314
s.add_impl_generic(parse_quote! { 'tcx });
3415
}
3516

36-
s.add_bounds(synstructure::AddBounds::Generics);
17+
s.add_bounds(synstructure::AddBounds::None);
18+
let mut where_clauses = None;
19+
s.add_trait_bounds(
20+
&parse_quote!(
21+
::rustc_middle::ty::traverse::OptVisitWith::<::rustc_middle::ty::TyCtxt<'tcx>>
22+
),
23+
&mut where_clauses,
24+
synstructure::AddBounds::Fields,
25+
);
26+
for pred in where_clauses.into_iter().flat_map(|c| c.predicates) {
27+
s.add_where_predicate(pred);
28+
}
29+
3730
let body_visit = s.each(|bind| {
3831
quote! {
3932
match ::rustc_ast_ir::visit::VisitorResult::branch(
40-
::rustc_middle::ty::visit::TypeVisitable::visit_with(#bind, __visitor)
33+
::rustc_middle::ty::traverse::OptVisitWith::mk_visit_with()(#bind, __visitor)
4134
) {
4235
::core::ops::ControlFlow::Continue(()) => {},
4336
::core::ops::ControlFlow::Break(r) => {

compiler/rustc_middle/src/macros.rs

+2-26
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,8 @@ macro_rules! TrivialLiftImpls {
7373
macro_rules! TrivialTypeTraversalImpls {
7474
($($ty:ty),+ $(,)?) => {
7575
$(
76-
impl<'tcx> $crate::ty::fold::TypeFoldable<$crate::ty::TyCtxt<'tcx>> for $ty {
77-
fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$crate::ty::TyCtxt<'tcx>>>(
78-
self,
79-
_: &mut F,
80-
) -> ::std::result::Result<Self, F::Error> {
81-
Ok(self)
82-
}
83-
84-
#[inline]
85-
fn fold_with<F: $crate::ty::fold::TypeFolder<$crate::ty::TyCtxt<'tcx>>>(
86-
self,
87-
_: &mut F,
88-
) -> Self {
89-
self
90-
}
91-
}
92-
93-
impl<'tcx> $crate::ty::visit::TypeVisitable<$crate::ty::TyCtxt<'tcx>> for $ty {
94-
#[inline]
95-
fn visit_with<F: $crate::ty::visit::TypeVisitor<$crate::ty::TyCtxt<'tcx>>>(
96-
&self,
97-
_: &mut F)
98-
-> F::Result
99-
{
100-
<F::Result as ::rustc_ast_ir::visit::VisitorResult>::output()
101-
}
76+
impl<'tcx> $crate::ty::traverse::TypeTraversable<$crate::ty::TyCtxt<'tcx>> for $ty {
77+
type Kind = $crate::ty::traverse::NoopTypeTraversal;
10278
}
10379
)+
10480
};

compiler/rustc_middle/src/mir/query.rs

-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ pub struct CoroutineLayout<'tcx> {
5757
/// Which saved locals are storage-live at the same time. Locals that do not
5858
/// have conflicts with each other are allowed to overlap in the computed
5959
/// layout.
60-
#[type_foldable(identity)]
61-
#[type_visitable(ignore)]
6260
pub storage_conflicts: BitMatrix<CoroutineSavedLocal, CoroutineSavedLocal>,
6361
}
6462

compiler/rustc_middle/src/mir/type_foldable.rs

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! `TypeFoldable` implementations for MIR types
2-
3-
use rustc_ast::InlineAsmTemplatePiece;
4-
use rustc_hir::def_id::LocalDefId;
2+
use rustc_index::bit_set::BitMatrix;
53

64
use super::*;
75

@@ -20,40 +18,14 @@ TrivialTypeTraversalImpls! {
2018
SwitchTargets,
2119
CoroutineKind,
2220
CoroutineSavedLocal,
21+
BitMatrix<CoroutineSavedLocal, CoroutineSavedLocal>,
2322
}
2423

2524
TrivialTypeTraversalImpls! {
2625
ConstValue<'tcx>,
2726
NullOp<'tcx>,
2827
}
2928

30-
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx [InlineAsmTemplatePiece] {
31-
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
32-
self,
33-
_folder: &mut F,
34-
) -> Result<Self, F::Error> {
35-
Ok(self)
36-
}
37-
}
38-
39-
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx [Span] {
40-
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
41-
self,
42-
_folder: &mut F,
43-
) -> Result<Self, F::Error> {
44-
Ok(self)
45-
}
46-
}
47-
48-
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<LocalDefId> {
49-
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
50-
self,
51-
_folder: &mut F,
52-
) -> Result<Self, F::Error> {
53-
Ok(self)
54-
}
55-
}
56-
5729
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<PlaceElem<'tcx>> {
5830
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
5931
self,

compiler/rustc_middle/src/thir.rs

-3
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,7 @@ pub enum PatKind<'tcx> {
739739
/// `x`, `ref x`, `x @ P`, etc.
740740
Binding {
741741
name: Symbol,
742-
#[type_visitable(ignore)]
743742
mode: BindingMode,
744-
#[type_visitable(ignore)]
745743
var: LocalVarId,
746744
ty: Ty<'tcx>,
747745
subpattern: Option<Box<Pat<'tcx>>>,
@@ -844,7 +842,6 @@ pub struct PatRange<'tcx> {
844842
pub lo: PatRangeBoundary<'tcx>,
845843
/// Must not be `NegInfinity`.
846844
pub hi: PatRangeBoundary<'tcx>,
847-
#[type_visitable(ignore)]
848845
pub end: RangeEnd,
849846
pub ty: Ty<'tcx>,
850847
}

compiler/rustc_middle/src/ty/consts/valtree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
1+
use rustc_macros::{HashStable, NoopTypeTraversable, TyDecodable, TyEncodable};
22

33
use super::ScalarInt;
44
use crate::mir::interpret::Scalar;
55
use crate::ty::{self, Ty, TyCtxt};
66

77
#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq)]
8-
#[derive(HashStable)]
8+
#[derive(HashStable, NoopTypeTraversable)]
99
/// This datastructure is used to represent the value of constants used in the type system.
1010
///
1111
/// We explicitly choose a different datastructure from the way values are processed within

compiler/rustc_middle/src/ty/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub use rustc_type_ir::ConstKind::{
5555
Placeholder as PlaceholderCt, Unevaluated, Value,
5656
};
5757
pub use rustc_type_ir::relate::VarianceDiagInfo;
58+
use rustc_type_ir::traverse::TypeTraversable;
5859
pub use rustc_type_ir::*;
5960
use tracing::{debug, instrument};
6061
pub use vtable::*;
@@ -1031,15 +1032,15 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ParamEnv<'tcx> {
10311032
) -> Result<Self, F::Error> {
10321033
Ok(ParamEnv::new(
10331034
self.caller_bounds().try_fold_with(folder)?,
1034-
self.reveal().try_fold_with(folder)?,
1035+
self.reveal().noop_try_fold_with(folder)?,
10351036
))
10361037
}
10371038
}
10381039

10391040
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ParamEnv<'tcx> {
10401041
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
10411042
try_visit!(self.caller_bounds().visit_with(visitor));
1042-
self.reveal().visit_with(visitor)
1043+
self.reveal().noop_visit_with(visitor)
10431044
}
10441045
}
10451046

compiler/rustc_middle/src/ty/structural_impls.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::def::Namespace;
1111
use rustc_span::source_map::Spanned;
1212
use rustc_target::abi::TyAndLayout;
1313
use rustc_type_ir::ConstKind;
14+
use rustc_type_ir::traverse::TypeTraversable;
1415

1516
use super::print::PrettyPrinter;
1617
use super::{GenericArg, GenericArgKind, Pattern, Region};
@@ -229,6 +230,7 @@ TrivialTypeTraversalImpls! {
229230
::rustc_hir::def_id::LocalDefId,
230231
::rustc_hir::ByRef,
231232
::rustc_hir::HirId,
233+
::rustc_hir::RangeEnd,
232234
::rustc_hir::MatchSource,
233235
::rustc_target::asm::InlineAsmRegOrRegClass,
234236
crate::mir::coverage::BlockMarkerId,
@@ -264,6 +266,8 @@ TrivialTypeTraversalImpls! {
264266
// interners).
265267
TrivialTypeTraversalAndLiftImpls! {
266268
::rustc_hir::def_id::DefId,
269+
::rustc_hir::Safety,
270+
::rustc_target::spec::abi::Abi,
267271
crate::ty::ClosureKind,
268272
crate::ty::ParamConst,
269273
crate::ty::ParamTy,
@@ -274,11 +278,6 @@ TrivialTypeTraversalAndLiftImpls! {
274278
rustc_target::abi::Size,
275279
}
276280

277-
TrivialLiftImpls! {
278-
::rustc_hir::Safety,
279-
::rustc_target::spec::abi::Abi,
280-
}
281-
282281
///////////////////////////////////////////////////////////////////////////
283282
// Lift implementations
284283

@@ -573,7 +572,7 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
573572
ConstKind::Placeholder(p) => ConstKind::Placeholder(p.try_fold_with(folder)?),
574573
ConstKind::Unevaluated(uv) => ConstKind::Unevaluated(uv.try_fold_with(folder)?),
575574
ConstKind::Value(t, v) => {
576-
ConstKind::Value(t.try_fold_with(folder)?, v.try_fold_with(folder)?)
575+
ConstKind::Value(t.try_fold_with(folder)?, v.noop_try_fold_with(folder)?)
577576
}
578577
ConstKind::Error(e) => ConstKind::Error(e.try_fold_with(folder)?),
579578
ConstKind::Expr(e) => ConstKind::Expr(e.try_fold_with(folder)?),
@@ -644,7 +643,7 @@ impl<'tcx, T: TypeVisitable<TyCtxt<'tcx>> + Debug + Clone> TypeVisitable<TyCtxt<
644643
{
645644
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
646645
try_visit!(self.node.visit_with(visitor));
647-
self.span.visit_with(visitor)
646+
self.span.noop_visit_with(visitor)
648647
}
649648
}
650649

@@ -657,7 +656,7 @@ impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + Debug + Clone> TypeFoldable<TyCtxt<'t
657656
) -> Result<Self, F::Error> {
658657
Ok(Spanned {
659658
node: self.node.try_fold_with(folder)?,
660-
span: self.span.try_fold_with(folder)?,
659+
span: self.span.noop_try_fold_with(folder)?,
661660
})
662661
}
663662
}

0 commit comments

Comments
 (0)