Skip to content

Commit 397cf05

Browse files
committed
Auto merge of #135272 - BoxyUwU:generic_arg_infer_reliability_2, r=<try>
[PERF] dont represent infer vars two different ways in hir visitors
2 parents a580b5c + c19e8c3 commit 397cf05

File tree

73 files changed

+1023
-490
lines changed

Some content is hidden

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

73 files changed

+1023
-490
lines changed

compiler/rustc_ast/src/ast.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_data_structures::packed::Pu128;
2828
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2929
use rustc_data_structures::stack::ensure_sufficient_stack;
3030
use rustc_data_structures::sync::Lrc;
31+
use rustc_data_structures::tagged_ptr::Tag;
3132
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
3233
pub use rustc_span::AttrId;
3334
use rustc_span::source_map::{Spanned, respan};
@@ -2269,10 +2270,32 @@ impl TyKind {
22692270

22702271
/// Syntax used to declare a trait object.
22712272
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
2273+
#[repr(u8)]
22722274
pub enum TraitObjectSyntax {
2273-
Dyn,
2274-
DynStar,
2275-
None,
2275+
// SAFETY: When adding new variants make sure to update the `Tag` impl.
2276+
Dyn = 0,
2277+
DynStar = 1,
2278+
None = 2,
2279+
}
2280+
2281+
/// SAFETY: `TraitObjectSyntax` only has 3 data-less variants which means
2282+
/// it can be represented with a `u2`. We use `repr(u8)` to guarantee the
2283+
/// discriminants of the variants are no greater than `3`.
2284+
unsafe impl Tag for TraitObjectSyntax {
2285+
const BITS: u32 = 2;
2286+
2287+
fn into_usize(self) -> usize {
2288+
self as u8 as usize
2289+
}
2290+
2291+
unsafe fn from_usize(tag: usize) -> Self {
2292+
match tag {
2293+
0 => TraitObjectSyntax::Dyn,
2294+
1 => TraitObjectSyntax::DynStar,
2295+
2 => TraitObjectSyntax::None,
2296+
_ => unreachable!(),
2297+
}
2298+
}
22762299
}
22772300

22782301
#[derive(Clone, Encodable, Decodable, Debug)]

compiler/rustc_ast_lowering/src/index.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use intravisit::InferKind;
12
use rustc_data_structures::sorted_map::SortedMap;
23
use rustc_hir as hir;
34
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
@@ -250,14 +251,6 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
250251
});
251252
}
252253

253-
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir>) {
254-
self.insert(const_arg.span(), const_arg.hir_id, Node::ConstArg(const_arg));
255-
256-
self.with_parent(const_arg.hir_id, |this| {
257-
intravisit::walk_const_arg(this, const_arg);
258-
});
259-
}
260-
261254
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
262255
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
263256

@@ -287,22 +280,41 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
287280
intravisit::walk_path_segment(self, path_segment);
288281
}
289282

290-
fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
291-
self.insert(ty.span, ty.hir_id, Node::Ty(ty));
283+
fn visit_ty(&mut self, ty: &'hir Ty<'hir, AmbigArg>) {
284+
self.insert(ty.span, ty.hir_id, Node::Ty(ty.as_unambig_ty()));
292285

293286
self.with_parent(ty.hir_id, |this| {
294-
intravisit::walk_ty(this, ty);
287+
intravisit::walk_ambig_ty(this, ty);
295288
});
296289
}
297290

298-
fn visit_infer(&mut self, inf: &'hir InferArg) {
299-
self.insert(inf.span, inf.hir_id, Node::Infer(inf));
291+
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir, AmbigArg>) {
292+
self.insert(
293+
const_arg.as_unambig_ct().span(),
294+
const_arg.hir_id,
295+
Node::ConstArg(const_arg.as_unambig_ct()),
296+
);
300297

301-
self.with_parent(inf.hir_id, |this| {
302-
intravisit::walk_inf(this, inf);
298+
self.with_parent(const_arg.hir_id, |this| {
299+
intravisit::walk_ambig_const_arg(this, const_arg);
303300
});
304301
}
305302

303+
fn visit_shared_ty_const(
304+
&mut self,
305+
inf_id: HirId,
306+
inf_span: Span,
307+
kind: InferKind<'hir>,
308+
) -> Self::Result {
309+
match kind {
310+
InferKind::Ty(ty) => self.insert(inf_span, inf_id, Node::Ty(ty)),
311+
InferKind::Const(ct) => self.insert(inf_span, inf_id, Node::ConstArg(ct)),
312+
InferKind::Ambig(inf) => self.insert(inf_span, inf_id, Node::Infer(inf)),
313+
}
314+
315+
self.visit_id(inf_id);
316+
}
317+
306318
fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
307319
self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
308320

compiler/rustc_ast_lowering/src/lib.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
4747
use rustc_data_structures::sorted_map::SortedMap;
4848
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4949
use rustc_data_structures::sync::Lrc;
50+
use rustc_data_structures::tagged_ptr::CovariantCopyTaggedPtr;
5051
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5152
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5253
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
@@ -1083,7 +1084,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10831084
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)),
10841085
ast::GenericArg::Type(ty) => {
10851086
match &ty.kind {
1086-
TyKind::Infer if self.tcx.features().generic_arg_infer() => {
1087+
TyKind::Infer => {
10871088
return GenericArg::Infer(hir::InferArg {
10881089
hir_id: self.lower_node_id(ty.id),
10891090
span: self.lower_span(ty.span),
@@ -1109,15 +1110,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11091110

11101111
let ct =
11111112
self.lower_const_path_to_const_arg(path, res, ty.id, ty.span);
1112-
return GenericArg::Const(ct);
1113+
return GenericArg::Const(ct.as_ambig_ct());
11131114
}
11141115
}
11151116
}
11161117
_ => {}
11171118
}
1118-
GenericArg::Type(self.lower_ty(ty, itctx))
1119+
GenericArg::Type(self.lower_ty(ty, itctx).as_ambig_ty())
1120+
}
1121+
ast::GenericArg::Const(ct) => {
1122+
GenericArg::Const(self.lower_anon_const_to_const_arg(ct).as_ambig_ct())
11191123
}
1120-
ast::GenericArg::Const(ct) => GenericArg::Const(self.lower_anon_const_to_const_arg(ct)),
11211124
}
11221125
}
11231126

@@ -1157,7 +1160,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11571160
let lifetime_bound = this.elided_dyn_bound(t.span);
11581161
(bounds, lifetime_bound)
11591162
});
1160-
let kind = hir::TyKind::TraitObject(bounds, lifetime_bound, TraitObjectSyntax::None);
1163+
let kind = hir::TyKind::TraitObject(
1164+
bounds,
1165+
CovariantCopyTaggedPtr::new(lifetime_bound, TraitObjectSyntax::None),
1166+
);
11611167
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
11621168
}
11631169

@@ -1184,7 +1190,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11841190

11851191
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
11861192
let kind = match &t.kind {
1187-
TyKind::Infer => hir::TyKind::Infer,
1193+
TyKind::Infer => hir::TyKind::Infer(()),
11881194
TyKind::Err(guar) => hir::TyKind::Err(*guar),
11891195
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
11901196
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
@@ -1308,7 +1314,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13081314
lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span));
13091315
(bounds, lifetime_bound)
13101316
});
1311-
hir::TyKind::TraitObject(bounds, lifetime_bound, *kind)
1317+
hir::TyKind::TraitObject(bounds, CovariantCopyTaggedPtr::new(lifetime_bound, *kind))
13121318
}
13131319
TyKind::ImplTrait(def_node_id, bounds) => {
13141320
let span = t.span;
@@ -2040,7 +2046,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20402046
)
20412047
.stash(c.value.span, StashKey::UnderscoreForArrayLengths);
20422048
}
2043-
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span));
2049+
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span), ());
20442050
self.arena.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
20452051
}
20462052
_ => self.lower_anon_const_to_const_arg(c),
@@ -2364,8 +2370,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23642370
hir_id = self.next_id();
23652371
hir::TyKind::TraitObject(
23662372
arena_vec![self; principal],
2367-
self.elided_dyn_bound(span),
2368-
TraitObjectSyntax::None,
2373+
CovariantCopyTaggedPtr::new(
2374+
self.elided_dyn_bound(span),
2375+
TraitObjectSyntax::None,
2376+
),
23692377
)
23702378
}
23712379
_ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),

compiler/rustc_ast_lowering/src/path.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
525525
}
526526
FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
527527
};
528-
let args = smallvec![GenericArg::Type(self.arena.alloc(self.ty_tup(*inputs_span, inputs)))];
528+
let args = smallvec![GenericArg::Type(
529+
self.arena.alloc(self.ty_tup(*inputs_span, inputs)).as_ambig_ty()
530+
)];
529531

530532
// If we have a bound like `async Fn() -> T`, make sure that we mark the
531533
// `Output = T` associated type bound with the right feature gates.

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::QPath::Resolved;
88
use rustc_hir::WherePredicateKind::BoundPredicate;
99
use rustc_hir::def::Res::Def;
1010
use rustc_hir::def_id::DefId;
11-
use rustc_hir::intravisit::Visitor;
11+
use rustc_hir::intravisit::VisitorExt;
1212
use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate};
1313
use rustc_infer::infer::{NllRegionVariableOrigin, RelateParamBound};
1414
use rustc_middle::bug;
@@ -887,7 +887,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
887887
if alias_ty.span.desugaring_kind().is_some() {
888888
// Skip `async` desugaring `impl Future`.
889889
}
890-
if let TyKind::TraitObject(_, lt, _) = alias_ty.kind {
890+
if let TyKind::TraitObject(_, lt) = alias_ty.kind {
891891
if lt.ident.name == kw::Empty {
892892
spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
893893
} else {
@@ -987,7 +987,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
987987
for found_did in found_dids {
988988
let mut traits = vec![];
989989
let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
990-
hir_v.visit_ty(self_ty);
990+
hir_v.visit_unambig_ty(self_ty);
991991
debug!("trait spans found: {:?}", traits);
992992
for span in &traits {
993993
let mut multi_span: MultiSpan = vec![*span].into();

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
432432
// must highlight the variable.
433433
// NOTE(eddyb) this is handled in/by the sole caller
434434
// (`give_name_if_anonymous_region_appears_in_arguments`).
435-
hir::TyKind::Infer => None,
435+
hir::TyKind::Infer(()) => None,
436436

437437
_ => Some(argument_hir_ty),
438438
}
@@ -615,7 +615,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
615615
}
616616

617617
(GenericArgKind::Type(ty), hir::GenericArg::Type(hir_ty)) => {
618-
search_stack.push((ty, hir_ty));
618+
search_stack.push((ty, hir_ty.as_unambig_ty()));
619619
}
620620

621621
(GenericArgKind::Const(_ct), hir::GenericArg::Const(_hir_ct)) => {

compiler/rustc_data_structures/src/marker.rs

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl_dyn_send!(
7373
[Box<T, A> where T: ?Sized + DynSend, A: std::alloc::Allocator + DynSend]
7474
[crate::sync::RwLock<T> where T: DynSend]
7575
[crate::tagged_ptr::CopyTaggedPtr<P, T, CP> where P: Send + crate::tagged_ptr::Pointer, T: Send + crate::tagged_ptr::Tag, const CP: bool]
76+
[crate::tagged_ptr::CovariantCopyTaggedPtr<P, T, CP> where P: Send + crate::tagged_ptr::Pointer<Target: Sized>, T: Send + crate::tagged_ptr::Tag, const CP: bool]
7677
[rustc_arena::TypedArena<T> where T: DynSend]
7778
[indexmap::IndexSet<V, S> where V: DynSend, S: DynSend]
7879
[indexmap::IndexMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
@@ -149,6 +150,7 @@ impl_dyn_sync!(
149150
[crate::sync::WorkerLocal<T> where T: DynSend]
150151
[crate::intern::Interned<'a, T> where 'a, T: DynSync]
151152
[crate::tagged_ptr::CopyTaggedPtr<P, T, CP> where P: Sync + crate::tagged_ptr::Pointer, T: Sync + crate::tagged_ptr::Tag, const CP: bool]
153+
[crate::tagged_ptr::CovariantCopyTaggedPtr<P, T, CP> where P: Sync + crate::tagged_ptr::Pointer<Target: Sized>, T: Sync + crate::tagged_ptr::Tag, const CP: bool]
152154
[parking_lot::lock_api::Mutex<R, T> where R: DynSync, T: ?Sized + DynSend]
153155
[parking_lot::lock_api::RwLock<R, T> where R: DynSync, T: ?Sized + DynSend + DynSync]
154156
[indexmap::IndexSet<V, S> where V: DynSync, S: DynSync]

compiler/rustc_data_structures/src/tagged_ptr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ use std::sync::Arc;
2323
use crate::aligned::Aligned;
2424

2525
mod copy;
26+
mod covariant;
2627
mod drop;
2728
mod impl_tag;
2829

2930
pub use copy::CopyTaggedPtr;
31+
pub use covariant::CovariantCopyTaggedPtr;
3032
pub use drop::TaggedPtr;
3133

3234
/// This describes the pointer type encapsulated by [`TaggedPtr`] and

0 commit comments

Comments
 (0)