Skip to content

Make some traversable types generic over interner #108214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3c58c0e
Improve docs of traversable derivation macros
eggyal Feb 26, 2023
1a9c3e5
Generify traversable derivation macros
eggyal Feb 26, 2023
09239c2
Automatically skip traversal of boring fields
eggyal Feb 25, 2023
d2ec961
Use specialisation in ParamEnv traversable impls
eggyal Feb 26, 2023
99e070f
Derive traversable impls for UserTypeProjection
eggyal Feb 25, 2023
408ba98
Derive traversable impls for BindingForm
eggyal Feb 25, 2023
ef97776
Derive traversable impls for Obligation
eggyal Feb 26, 2023
5d4dd4b
Simplify traversable impls for ExternalConstraints
eggyal Feb 25, 2023
b75b00d
Remove superfluous traversable impls
eggyal Feb 25, 2023
8f8a920
Remove superfluous traversable impl for usize
eggyal Feb 25, 2023
4e4b936
Remove superfluous traversable impl for bool
eggyal Feb 25, 2023
e3f58d0
Newtype for FakeRead semi-traversable tuple
eggyal Mar 20, 2023
feaeef5
Newtype for AscribeUserType semi-traversable tuple
eggyal Mar 20, 2023
c79f135
Newtypes not DefId and LocalDefId traversable impl
eggyal Mar 20, 2023
6833eea
Use rustc_type_ir directly in derived traversables
eggyal Feb 25, 2023
8da36bd
Derive traversables over generic interners
eggyal Mar 21, 2023
d1cc6e5
Use Spanned not semi-traversable Span tuples
eggyal Mar 20, 2023
9b8fcc5
Make no-op traversables generic over the interner
eggyal Mar 21, 2023
85f0500
Replace TrivialTypeTraversal macros with derives
eggyal Feb 25, 2023
b0ccdac
Unimpl TypeSuperVisitable for UnevaluatedConst
eggyal Feb 26, 2023
11296fa
Generify traversals of interned types
eggyal Feb 26, 2023
bf618ac
Explain no-op traversable impls
eggyal Mar 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4693,6 +4693,7 @@ dependencies = [
"rustc_serialize",
"rustc_span",
"rustc_target",
"rustc_type_ir",
"smallvec",
"tracing",
]
Expand Down Expand Up @@ -4803,6 +4804,7 @@ dependencies = [
"rustc_serialize",
"rustc_span",
"rustc_target",
"rustc_type_ir",
"smallvec",
"tracing",
]
Expand Down Expand Up @@ -5300,6 +5302,7 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_serialize",
"rustc_type_ir",
"scoped-tls",
"sha1",
"sha2",
Expand Down Expand Up @@ -5338,6 +5341,7 @@ dependencies = [
"rustc_macros",
"rustc_serialize",
"rustc_span",
"rustc_type_ir",
"serde_json",
"tracing",
]
Expand Down Expand Up @@ -5369,6 +5373,7 @@ dependencies = [
"rustc_span",
"rustc_target",
"rustc_transmute",
"rustc_type_ir",
"smallvec",
"tracing",
]
Expand Down Expand Up @@ -5405,6 +5410,7 @@ dependencies = [
"rustc_middle",
"rustc_span",
"rustc_target",
"rustc_type_ir",
"tracing",
]

Expand Down
16 changes: 9 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use rustc_infer::traits::ObligationCause;
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::{
self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, ConstraintCategory,
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
FakeReadCause, FakeReadCauseAndPlace, LocalDecl, LocalInfo, LocalKind, Location, Operand,
Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
VarBindingForm,
};
use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty};
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
Expand Down Expand Up @@ -670,8 +671,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let tcx = self.infcx.tcx;

// Find out if the predicates show that the type is a Fn or FnMut
let find_fn_kind_from_did = |(pred, _): (ty::Predicate<'tcx>, _)| {
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = pred.kind().skip_binder()
let find_fn_kind_from_did = |pred: ty::Spanned<ty::Predicate<'tcx>>| {
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = pred.node.kind().skip_binder()
&& pred.self_ty() == ty
{
if Some(pred.def_id()) == tcx.lang_items().fn_trait() {
Expand Down Expand Up @@ -2578,9 +2579,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
impl<'tcx> Visitor<'tcx> for FakeReadCauseFinder<'tcx> {
fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) {
match statement {
Statement { kind: StatementKind::FakeRead(box (cause, place)), .. }
if *place == self.place =>
{
Statement {
kind: StatementKind::FakeRead(box FakeReadCauseAndPlace(cause, place)),
..
} if *place == self.place => {
self.cause = Some(*cause);
}
_ => (),
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rustc_hir::intravisit::Visitor;
use rustc_index::vec::IndexVec;
use rustc_infer::infer::NllRegionVariableOrigin;
use rustc_middle::mir::{
Body, CastKind, ConstraintCategory, FakeReadCause, Local, LocalInfo, Location, Operand, Place,
Rvalue, Statement, StatementKind, TerminatorKind,
Body, CastKind, ConstraintCategory, FakeReadCause, FakeReadCauseAndPlace, Local, LocalInfo,
Location, Operand, Place, Rvalue, Statement, StatementKind, TerminatorKind,
};
use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::{self, RegionVid, TyCtxt};
Expand Down Expand Up @@ -482,7 +482,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let block = &self.body.basic_blocks[location.block];

let kind = if let Some(&Statement {
kind: StatementKind::FakeRead(box (FakeReadCause::ForLet(_), place)),
kind:
StatementKind::FakeRead(box FakeReadCauseAndPlace(
FakeReadCause::ForLet(_),
place,
)),
..
}) = block.statements.get(location.statement_index)
{
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use rustc_hir::GeneratorKind;
use rustc_infer::infer::{LateBoundRegionConversionTime, TyCtxtInferExt};
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::{
AggregateKind, Constant, FakeReadCause, Field, Local, LocalInfo, LocalKind, Location, Operand,
Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
AggregateKind, Constant, FakeReadCause, FakeReadCauseAndPlace, Field, Local, LocalInfo,
LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement,
StatementKind, Terminator, TerminatorKind,
};
use rustc_middle::ty::print::Print;
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
Expand Down Expand Up @@ -818,7 +819,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {

// StatementKind::FakeRead only contains a def_id if they are introduced as a result
// of pattern matching within a closure.
if let StatementKind::FakeRead(box (cause, place)) = stmt.kind {
if let StatementKind::FakeRead(box FakeReadCauseAndPlace(cause, place)) = stmt.kind {
match cause {
FakeReadCause::ForMatchedPlace(Some(closure_def_id))
| FakeReadCause::ForLet(Some(closure_def_id)) => {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
// opt_match_place is None for let [mut] x = ... statements,
// whether or not the right-hand side is a place expression
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
opt_match_place: Some((opt_match_place, match_span)),
opt_match_place:
Some(ty::Spanned { node: opt_match_place, span: match_span }),
binding_mode: _,
opt_ty_info: _,
pat_span: _,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/invalidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {

self.mutate_place(location, *lhs, Shallow(None));
}
StatementKind::FakeRead(box (_, _)) => {
StatementKind::FakeRead(_) => {
// Only relevant for initialized/liveness/safety checks.
}
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use rustc_infer::infer::{
};
use rustc_macros::fluent_messages;
use rustc_middle::mir::{
traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand,
Place, PlaceElem, PlaceRef, VarDebugInfoContents,
traversal, Body, ClearCrossCrate, FakeReadCauseAndPlace, Local, Location, Mutability,
NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef, VarDebugInfoContents,
};
use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
Expand Down Expand Up @@ -663,7 +663,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx

self.mutate_place(location, (*lhs, span), Shallow(None), flow_state);
}
StatementKind::FakeRead(box (_, place)) => {
StatementKind::FakeRead(box FakeReadCauseAndPlace(_, place)) => {
// Read for match doesn't access any memory and is used to
// assert that a place is safe and live. So we don't have to
// do any checks here.
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_borrowck/src/type_check/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
instantiated_predicates: ty::InstantiatedPredicates<'tcx>,
locations: Locations,
) {
for (predicate, span) in instantiated_predicates {
debug!(?predicate);
let category = ConstraintCategory::Predicate(span);
let predicate = self.normalize_with_category(predicate, locations, category);
for predicate in instantiated_predicates {
debug!(?predicate.node);
let category = ConstraintCategory::Predicate(predicate.span);
let predicate = self.normalize_with_category(predicate.node, locations, category);
self.prove_predicate(predicate, locations, category);
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
);
}
}
StatementKind::AscribeUserType(box (place, projection), variance) => {
StatementKind::AscribeUserType(box AscribeUserType(place, projection), variance) => {
let place_ty = place.ty(body, tcx).ty;
if let Err(terr) = self.relate_type_and_user_type(
place_ty,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_cranelift/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,13 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
loop {
let scope_data = &self.mir.source_scopes[source_info.scope];

if let Some((callee, callsite_span)) = scope_data.inlined {
if let Some(callee) = scope_data.inlined {
// Stop inside the most nested non-`#[track_caller]` function,
// before ever reaching its caller (which is irrelevant).
if !callee.def.requires_caller_location(self.tcx) {
if !callee.node.def.requires_caller_location(self.tcx) {
return span_to_caller_location(self, source_info.span);
}
source_info.span = callsite_span;
source_info.span = callee.span;
}

// Skip past all of the parents with `inlined: None`.
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ fn make_mir_scope<'ll, 'tcx>(
let file_metadata = file_metadata(cx, &loc.file);

let dbg_scope = match scope_data.inlined {
Some((callee, _)) => {
Some(callee) => {
// FIXME(eddyb) this would be `self.monomorphize(&callee)`
// if this is moved to `rustc_codegen_ssa::mir::debuginfo`.
let callee = cx.tcx.subst_and_normalize_erasing_regions(
instance.substs,
ty::ParamEnv::reveal_all(),
callee,
callee.node,
);
let callee_fn_abi = cx.fn_abi_of_instance(callee, ty::List::empty());
cx.dbg_scope_fn(callee, callee_fn_abi, None)
Expand All @@ -109,11 +109,11 @@ fn make_mir_scope<'ll, 'tcx>(
},
};

let inlined_at = scope_data.inlined.map(|(_, callsite_span)| {
let inlined_at = scope_data.inlined.map(|callee| {
// FIXME(eddyb) this doesn't account for the macro-related
// `Span` fixups that `rustc_codegen_ssa::mir::debuginfo` does.
let callsite_scope = parent_scope.adjust_dbg_scope_for_span(cx, callsite_span);
cx.dbg_loc(callsite_scope, parent_scope.inlined_at, callsite_span)
let callsite_scope = parent_scope.adjust_dbg_scope_for_span(cx, callee.span);
cx.dbg_loc(callsite_scope, parent_scope.inlined_at, callee.span)
});

debug_context.scopes[scope] = DebugScope {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1497,13 +1497,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
loop {
let scope_data = &self.mir.source_scopes[source_info.scope];

if let Some((callee, callsite_span)) = scope_data.inlined {
if let Some(callee) = scope_data.inlined {
// Stop inside the most nested non-`#[track_caller]` function,
// before ever reaching its caller (which is irrelevant).
if !callee.def.requires_caller_location(tcx) {
if !callee.node.def.requires_caller_location(tcx) {
return span_to_caller_location(source_info.span);
}
source_info.span = callsite_span;
source_info.span = callee.span;
}

// Skip past all of the parents with `inlined: None`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
loop {
let scope_data = &frame.body.source_scopes[source_info.scope];

if let Some((callee, callsite_span)) = scope_data.inlined {
if let Some(callee) = scope_data.inlined {
// Stop inside the most nested non-`#[track_caller]` function,
// before ever reaching its caller (which is irrelevant).
if !callee.def.requires_caller_location(*self.tcx) {
if !callee.node.def.requires_caller_location(*self.tcx) {
return source_info.span;
}
source_info.span = callsite_span;
source_info.span = callee.span;
}

// Skip past all of the parents with `inlined: None`.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_data_structures/src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ mod private {
#[rustc_pass_by_value]
pub struct Interned<'a, T>(pub &'a T, pub private::PrivateZst);

pub trait Internable<'a, I>: Sized {
fn intern(self, interner: I) -> Interned<'a, Self>;
}

impl<'a, T> Interned<'a, T> {
/// Create a new `Interned` value. The value referred to *must* be interned
/// and thus be unique, and it *must* remain unique in the future. This
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rustc_index = { path = "../rustc_index" }
rustc_span = { path = "../rustc_span" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_ast = { path = "../rustc_ast" }
rustc_type_ir = { path = "../rustc_type_ir" }
tracing = "0.1"
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
odht = { version = "0.3.1", features = ["nightly"] }
5 changes: 4 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3190,7 +3190,10 @@ impl<'hir> Item<'hir> {
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
#[derive(Encodable, Decodable, HashStable_Generic, TypeFoldable, TypeVisitable)]
#[skip_traversal(but_impl_because = "
`Unsafety` impls `Relate`, which is a subtrait of `TypeFoldable`.
")]
pub enum Unsafety {
Unsafe,
Normal,
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,20 +1314,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {

let mut trait_bounds = vec![];
let mut projection_bounds = vec![];
for (pred, span) in bounds.predicates() {
let bound_pred = pred.kind();
for pred in bounds.predicates() {
let bound_pred = pred.node.kind();
match bound_pred.skip_binder() {
ty::PredicateKind::Clause(clause) => match clause {
ty::Clause::Trait(trait_pred) => {
assert_eq!(trait_pred.polarity, ty::ImplPolarity::Positive);
trait_bounds.push((
bound_pred.rebind(trait_pred.trait_ref),
span,
pred.span,
trait_pred.constness,
));
}
ty::Clause::Projection(proj) => {
projection_bounds.push((bound_pred.rebind(proj), span));
projection_bounds.push((bound_pred.rebind(proj), pred.span));
}
ty::Clause::TypeOutlives(_) => {
// Do nothing, we deal with regions separately
Expand Down Expand Up @@ -1784,8 +1784,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|| {
traits::transitive_bounds_that_define_assoc_type(
tcx,
predicates.iter().filter_map(|(p, _)| {
Some(p.to_opt_poly_trait_pred()?.map_bound(|t| t.trait_ref))
predicates.iter().filter_map(|p| {
Some(p.node.to_opt_poly_trait_pred()?.map_bound(|t| t.trait_ref))
}),
assoc_name,
)
Expand Down
16 changes: 10 additions & 6 deletions compiler/rustc_hir_analysis/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use rustc_span::Span;
/// include the self type (e.g., `trait_bounds`) but in others we do not
#[derive(Default, PartialEq, Eq, Clone, Debug)]
pub struct Bounds<'tcx> {
pub predicates: Vec<(ty::Predicate<'tcx>, Span)>,
pub predicates: Vec<ty::Spanned<ty::Predicate<'tcx>>>,
}

impl<'tcx> Bounds<'tcx> {
Expand All @@ -33,7 +33,7 @@ impl<'tcx> Bounds<'tcx> {
region: ty::PolyTypeOutlivesPredicate<'tcx>,
span: Span,
) {
self.predicates.push((region.to_predicate(tcx), span));
self.predicates.push(ty::Spanned { node: region.to_predicate(tcx), span });
}

pub fn push_trait_bound(
Expand All @@ -43,7 +43,10 @@ impl<'tcx> Bounds<'tcx> {
span: Span,
constness: ty::BoundConstness,
) {
self.predicates.push((trait_ref.with_constness(constness).to_predicate(tcx), span));
self.predicates.push(ty::Spanned {
node: trait_ref.with_constness(constness).to_predicate(tcx),
span,
});
}

pub fn push_projection_bound(
Expand All @@ -52,17 +55,18 @@ impl<'tcx> Bounds<'tcx> {
projection: ty::PolyProjectionPredicate<'tcx>,
span: Span,
) {
self.predicates.push((projection.to_predicate(tcx), span));
self.predicates.push(ty::Spanned { node: projection.to_predicate(tcx), span });
}

pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) {
let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span));
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(sized_def_id, [ty]));
// Preferrable to put this obligation first, since we report better errors for sized ambiguity.
self.predicates.insert(0, (trait_ref.without_const().to_predicate(tcx), span));
self.predicates
.insert(0, ty::Spanned { node: trait_ref.without_const().to_predicate(tcx), span });
}

pub fn predicates(&self) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> + '_ {
pub fn predicates(&self) -> impl Iterator<Item = ty::Spanned<ty::Predicate<'tcx>>> + '_ {
self.predicates.iter().cloned()
}
}
Loading