Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 07bfd6b

Browse files
authoredMar 1, 2025
Rollup merge of rust-lang#137763 - compiler-errors:ty-nits, r=BoxyUwU
Use `mk_ty_from_kind` a bit less, clean up lifetime handling in borrowck r? `@BoxyUwU` Pulled out of my attempt to turn that `*const dyn Tr + '_` casting into a lint (which failed lmao)
2 parents 645a0fd + 371c073 commit 07bfd6b

File tree

4 files changed

+21
-37
lines changed

4 files changed

+21
-37
lines changed
 

‎compiler/rustc_borrowck/src/type_check/mod.rs

+13-31
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_mir_dataflow::move_paths::MoveData;
3838
use rustc_mir_dataflow::points::DenseLocationMap;
3939
use rustc_span::def_id::CRATE_DEF_ID;
4040
use rustc_span::source_map::Spanned;
41-
use rustc_span::{DUMMY_SP, Span, sym};
41+
use rustc_span::{Span, sym};
4242
use rustc_trait_selection::traits::query::type_op::custom::scrape_region_constraints;
4343
use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput};
4444
use tracing::{debug, instrument, trace};
@@ -51,7 +51,6 @@ use crate::polonius::legacy::{PoloniusFacts, PoloniusLocationTable};
5151
use crate::polonius::{PoloniusContext, PoloniusLivenessContext};
5252
use crate::region_infer::TypeTest;
5353
use crate::region_infer::values::{LivenessValues, PlaceholderIndex, PlaceholderIndices};
54-
use crate::renumber::RegionCtxt;
5554
use crate::session_diagnostics::{MoveUnsized, SimdIntrinsicArgConst};
5655
use crate::type_check::free_region_relations::{CreateResult, UniversalRegionRelations};
5756
use crate::universal_regions::{DefiningTy, UniversalRegions};
@@ -2121,35 +2120,31 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21212120
//
21222121
// Note that other checks (such as denying `dyn Send` -> `dyn
21232122
// Debug`) are in `rustc_hir_typeck`.
2124-
if let ty::Dynamic(src_tty, ..) = src_tail.kind()
2125-
&& let ty::Dynamic(dst_tty, ..) = dst_tail.kind()
2123+
if let ty::Dynamic(src_tty, _src_lt, _) = *src_tail.kind()
2124+
&& let ty::Dynamic(dst_tty, dst_lt, _) = *dst_tail.kind()
21262125
&& src_tty.principal().is_some()
21272126
&& dst_tty.principal().is_some()
21282127
{
21292128
// Remove auto traits.
21302129
// Auto trait checks are handled in `rustc_hir_typeck` as FCW.
2131-
let src_obj = tcx.mk_ty_from_kind(ty::Dynamic(
2130+
let src_obj = Ty::new_dynamic(
2131+
tcx,
21322132
tcx.mk_poly_existential_predicates(
21332133
&src_tty.without_auto_traits().collect::<Vec<_>>(),
21342134
),
2135-
tcx.lifetimes.re_static,
2135+
// FIXME: Once we disallow casting `*const dyn Trait + 'short`
2136+
// to `*const dyn Trait + 'long`, then this can just be `src_lt`.
2137+
dst_lt,
21362138
ty::Dyn,
2137-
));
2138-
let dst_obj = tcx.mk_ty_from_kind(ty::Dynamic(
2139+
);
2140+
let dst_obj = Ty::new_dynamic(
2141+
tcx,
21392142
tcx.mk_poly_existential_predicates(
21402143
&dst_tty.without_auto_traits().collect::<Vec<_>>(),
21412144
),
2142-
tcx.lifetimes.re_static,
2145+
dst_lt,
21432146
ty::Dyn,
2144-
));
2145-
2146-
// Replace trait object lifetimes with fresh vars, to allow
2147-
// casts like
2148-
// `*mut dyn FnOnce() + 'a` -> `*mut dyn FnOnce() + 'static`
2149-
let src_obj =
2150-
freshen_single_trait_object_lifetime(self.infcx, src_obj);
2151-
let dst_obj =
2152-
freshen_single_trait_object_lifetime(self.infcx, dst_obj);
2147+
);
21532148

21542149
debug!(?src_tty, ?dst_tty, ?src_obj, ?dst_obj);
21552150

@@ -2707,16 +2702,3 @@ impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
27072702
Ok(output)
27082703
}
27092704
}
2710-
2711-
fn freshen_single_trait_object_lifetime<'tcx>(
2712-
infcx: &BorrowckInferCtxt<'tcx>,
2713-
ty: Ty<'tcx>,
2714-
) -> Ty<'tcx> {
2715-
let &ty::Dynamic(tty, _, dyn_kind @ ty::Dyn) = ty.kind() else { bug!("expected trait object") };
2716-
2717-
let fresh = infcx
2718-
.next_region_var(rustc_infer::infer::RegionVariableOrigin::MiscVariable(DUMMY_SP), || {
2719-
RegionCtxt::Unknown
2720-
});
2721-
infcx.tcx.mk_ty_from_kind(ty::Dynamic(tty, fresh, dyn_kind))
2722-
}

‎compiler/rustc_hir_typeck/src/cast.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -895,20 +895,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
895895
// e.g. we want to allow `dyn T -> (dyn T,)`, etc.
896896
//
897897
// We also need to skip auto traits to emit an FCW and not an error.
898-
let src_obj = tcx.mk_ty_from_kind(ty::Dynamic(
898+
let src_obj = Ty::new_dynamic(
899+
tcx,
899900
tcx.mk_poly_existential_predicates(
900901
&src_tty.without_auto_traits().collect::<Vec<_>>(),
901902
),
902903
tcx.lifetimes.re_erased,
903904
ty::Dyn,
904-
));
905-
let dst_obj = tcx.mk_ty_from_kind(ty::Dynamic(
905+
);
906+
let dst_obj = Ty::new_dynamic(
907+
tcx,
906908
tcx.mk_poly_existential_predicates(
907909
&dst_tty.without_auto_traits().collect::<Vec<_>>(),
908910
),
909911
tcx.lifetimes.re_erased,
910912
ty::Dyn,
911-
));
913+
);
912914

913915
// `dyn Src = dyn Dst`, this checks for matching traits/generics/projections
914916
// This is `fcx.demand_eqtype`, but inlined to give a better error.

‎compiler/rustc_middle/src/ty/codec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for Ty<'tcx> {
224224
})
225225
} else {
226226
let tcx = decoder.interner();
227-
tcx.mk_ty_from_kind(rustc_type_ir::TyKind::decode(decoder))
227+
tcx.mk_ty_from_kind(ty::TyKind::decode(decoder))
228228
}
229229
}
230230
}

‎compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl<'tcx> Ty<'tcx> {
462462

463463
#[inline]
464464
pub fn new_param(tcx: TyCtxt<'tcx>, index: u32, name: Symbol) -> Ty<'tcx> {
465-
tcx.mk_ty_from_kind(Param(ParamTy { index, name }))
465+
Ty::new(tcx, Param(ParamTy { index, name }))
466466
}
467467

468468
#[inline]

0 commit comments

Comments
 (0)
Failed to load comments.