Skip to content

Commit cf04547

Browse files
committed
Address code review comments
1 parent de42ac3 commit cf04547

File tree

27 files changed

+199
-261
lines changed

27 files changed

+199
-261
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_session::lint::builtin::{
1919
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY,
2020
};
2121
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
22-
use rustc_session::parse::feature_err;
2322
use rustc_session::Session;
2423
use rustc_span::source_map::Spanned;
2524
use rustc_span::symbol::{kw, sym, Ident};
@@ -754,19 +753,7 @@ impl<'a> AstValidator<'a> {
754753
self.maybe_lint_missing_abi(sig_span, ty.id);
755754
}
756755
}
757-
TyKind::TraitObject(ref bounds, syntax, ..) => {
758-
if syntax == TraitObjectSyntax::DynStar
759-
&& !self.session.features_untracked().dyn_star
760-
{
761-
feature_err(
762-
&self.session.parse_sess,
763-
sym::dyn_star,
764-
ty.span,
765-
"dyn* trait objects are unstable",
766-
)
767-
.emit();
768-
}
769-
756+
TyKind::TraitObject(ref bounds, ..) => {
770757
let mut any_lifetime_bounds = false;
771758
for bound in bounds {
772759
if let GenericBound::Outlives(ref lifetime) = *bound {

compiler/rustc_ast_passes/src/feature_gate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
573573
ast::TyKind::Never => {
574574
gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
575575
}
576+
ast::TyKind::TraitObject(_, ast::TraitObjectSyntax::DynStar, ..) => {
577+
gate_feature_post!(&self, dyn_star, ty.span, "dyn* trait objects are unstable");
578+
}
576579
_ => {}
577580
}
578581
visit::walk_ty(self, ty)

compiler/rustc_codegen_cranelift/src/value_and_place.rs

+1
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ pub(crate) fn assert_assignable<'tcx>(
816816
// fn(&T) -> for<'l> fn(&'l T) is allowed
817817
}
818818
(&ty::Dynamic(from_traits, _, _from_kind), &ty::Dynamic(to_traits, _, _to_kind)) => {
819+
// FIXME(dyn-star): Do the right thing with DynKinds
819820
for (from, to) in from_traits.iter().zip(to_traits) {
820821
let from =
821822
fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from);

compiler/rustc_codegen_ssa/src/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
367367
bx.ret(llval);
368368
}
369369

370-
#[tracing::instrument(level = "debug", skip(self, helper, bx))]
370+
#[tracing::instrument(level = "trace", skip(self, helper, bx))]
371371
fn codegen_drop_terminator(
372372
&mut self,
373373
helper: TerminatorCodegenHelper<'tcx>,

compiler/rustc_const_eval/src/interpret/cast.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
113113
if let ty::Dynamic(data, _, ty::DynStar) = cast_ty.kind() {
114114
// Initial cast from sized to dyn trait
115115
let vtable = self.get_vtable_ptr(src.layout.ty, data.principal())?;
116-
let ptr = self.read_immediate(src)?.to_scalar();
117-
// FIXME(dyn-star): This should not use new_dyn_trait, but
118-
// it does exactly the same thing (makes a scalar pair)...
119-
// so maybe we should just duplicate/rename the function.
120-
let val = Immediate::new_dyn_trait(ptr, vtable, &*self.tcx);
116+
let vtable = Scalar::from_maybe_pointer(vtable, self);
117+
let data = self.read_immediate(src)?.to_scalar();
118+
let _assert_pointer_sized = data.to_pointer(self)?;
119+
let val = Immediate::ScalarPair(data, vtable);
121120
self.write_immediate(val, dest)?;
122121
} else {
123122
bug!()
@@ -327,7 +326,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
327326
let new_vptr = self.get_vtable_ptr(ty, data_b.principal())?;
328327
self.write_immediate(Immediate::new_dyn_trait(old_data, new_vptr, self), dest)
329328
}
330-
(_, &ty::Dynamic(ref data, _, _repr)) => {
329+
(_, &ty::Dynamic(ref data, _, ty::Dyn)) => {
331330
// Initial cast from sized to dyn trait
332331
let vtable = self.get_vtable_ptr(src_pointee_ty, data.principal())?;
333332
let ptr = self.read_scalar(src)?;

compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
570570
}
571571
}
572572
CastKind::DynStar => {
573-
// FIXME: make sure nothing needs to be done here.
573+
// FIXME(dyn-star): make sure nothing needs to be done here.
574574
}
575575
// Nothing to check here
576576
CastKind::PointerFromExposedAddress

compiler/rustc_feature/src/active.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ declare_features! (
381381
/// Allows `#[doc(masked)]`.
382382
(active, doc_masked, "1.21.0", Some(44027), None),
383383
/// Allows `dyn* Trait` objects.
384-
(active, dyn_star, "1.65.0", Some(91611), None),
384+
(incomplete, dyn_star, "CURRENT_RUSTC_VERSION", Some(91611), None),
385385
/// Allows `X..Y` patterns.
386386
(active, exclusive_range_pattern, "1.11.0", Some(37854), None),
387387
/// Allows exhaustive pattern matching on types that contain uninhabited types.

compiler/rustc_middle/src/ty/layout.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
626626
}
627627

628628
ty::Dynamic(_, _, ty::DynStar) => {
629-
let mut pointer = scalar_unit(Int(dl.ptr_sized_integer(), false));
630-
pointer.valid_range_mut().start = 1;
629+
let mut data = scalar_unit(Int(dl.ptr_sized_integer(), false));
630+
data.valid_range_mut().start = 0;
631631
let mut vtable = scalar_unit(Pointer);
632632
vtable.valid_range_mut().start = 1;
633-
tcx.intern_layout(self.scalar_pair(pointer, vtable))
633+
tcx.intern_layout(self.scalar_pair(data, vtable))
634634
}
635635

636636
// Arrays and slices.
@@ -2474,8 +2474,7 @@ where
24742474

24752475
match tcx.struct_tail_erasing_lifetimes(pointee, cx.param_env()).kind() {
24762476
ty::Slice(_) | ty::Str => TyMaybeWithLayout::Ty(tcx.types.usize),
2477-
// FIXME(eholk): Do the right thing with trait object representation
2478-
ty::Dynamic(_, _, _repr) => {
2477+
ty::Dynamic(_, _, ty::Dyn) => {
24792478
TyMaybeWithLayout::Ty(tcx.mk_imm_ref(
24802479
tcx.lifetimes.re_static,
24812480
tcx.mk_array(tcx.types.usize, 3),
@@ -3379,7 +3378,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
33793378
Ok(self.tcx.arena.alloc(fn_abi))
33803379
}
33813380

3382-
#[tracing::instrument(level = "debug", skip(self))]
3381+
#[tracing::instrument(level = "trace", skip(self))]
33833382
fn fn_abi_adjust_for_abi(
33843383
&self,
33853384
fn_abi: &mut FnAbi<'tcx, Ty<'tcx>>,

compiler/rustc_monomorphize/src/collector.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,9 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
699699
// This could also be a different Unsize instruction, like
700700
// from a fixed sized array to a slice. But we are only
701701
// interested in things that produce a vtable.
702-
if (target_ty.is_trait() || target_ty.is_dyn_star()) && !source_ty.is_trait() {
702+
if (target_ty.is_trait() && !source_ty.is_trait())
703+
|| (target_ty.is_dyn_star() && !source_ty.is_dyn_star())
704+
{
703705
create_mono_items_for_vtable_methods(
704706
self.tcx,
705707
target_ty,

compiler/rustc_parse/src/parser/ty.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,7 @@ impl<'a> Parser<'a> {
579579
self.bump(); // `dyn`
580580

581581
// parse dyn* types
582-
let dyn_star = matches!(self.token.kind, TokenKind::BinOp(token::Star));
583-
let syntax = if dyn_star {
584-
self.bump(); // `*`
582+
let syntax = if self.eat(&TokenKind::BinOp(token::Star)) {
585583
TraitObjectSyntax::DynStar
586584
} else {
587585
TraitObjectSyntax::Dyn

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,13 @@ fn encode_ty<'tcx>(
627627
}
628628

629629
// Trait types
630-
ty::Dynamic(predicates, region, _repr) => {
630+
ty::Dynamic(predicates, region, kind) => {
631631
// u3dynI<element-type1[..element-typeN]>E, where <element-type> is <predicate>, as
632632
// vendor extended type.
633-
let mut s = String::from("u3dynI");
633+
let mut s = String::from(match kind {
634+
ty::Dyn => "u3dynI",
635+
ty::DynStar => "u7dynstarI",
636+
});
634637
s.push_str(&encode_predicates(tcx, predicates, dict, options));
635638
s.push_str(&encode_region(tcx, *region, dict, options));
636639
s.push('E');

compiler/rustc_symbol_mangling/src/v0.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,12 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
479479
})?;
480480
}
481481

482-
ty::Dynamic(predicates, r, _repr) => {
483-
self.push("D");
482+
ty::Dynamic(predicates, r, kind) => {
483+
self.push(match kind {
484+
ty::Dyn => "D",
485+
// FIXME(dyn-star): need to update v0 mangling docs
486+
ty::DynStar => "D*",
487+
});
484488
self = self.print_dyn_existential(predicates)?;
485489
self = r.print(self)?;
486490
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10671067
self_ty: Ty<'tcx>,
10681068
object_ty: Ty<'tcx>,
10691069
) {
1070-
let ty::Dynamic(predicates, _, _) = object_ty.kind() else { return; };
1070+
let ty::Dynamic(predicates, _, ty::Dyn) = object_ty.kind() else { return; };
10711071
let self_ref_ty = self.tcx.mk_imm_ref(self.tcx.lifetimes.re_erased, self_ty);
10721072

10731073
for predicate in predicates.iter() {
@@ -1365,7 +1365,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13651365
let trait_pred = self.resolve_vars_if_possible(trait_pred);
13661366
let ty = trait_pred.skip_binder().self_ty();
13671367
let is_object_safe = match ty.kind() {
1368-
ty::Dynamic(predicates, _, _) => {
1368+
ty::Dynamic(predicates, _, ty::Dyn) => {
13691369
// If the `dyn Trait` is not object safe, do not suggest `Box<dyn Trait>`.
13701370
predicates
13711371
.principal_def_id()

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
890890
let mut nested = vec![];
891891
match (source.kind(), target.kind()) {
892892
// Trait+Kx+'a -> Trait+Ky+'b (auto traits and lifetime subtyping).
893-
(&ty::Dynamic(ref data_a, r_a, repr_a), &ty::Dynamic(ref data_b, r_b, repr_b))
894-
if repr_a == repr_b =>
895-
{
893+
(&ty::Dynamic(ref data_a, r_a, ty::Dyn), &ty::Dynamic(ref data_b, r_b, ty::Dyn)) => {
896894
// See `assemble_candidates_for_unsizing` for more info.
897895
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
898896
let iter = data_a
@@ -911,7 +909,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
911909
.map(ty::Binder::dummy),
912910
);
913911
let existential_predicates = tcx.mk_poly_existential_predicates(iter);
914-
let source_trait = tcx.mk_dynamic(existential_predicates, r_b, repr_b);
912+
let source_trait = tcx.mk_dynamic(existential_predicates, r_b, ty::Dyn);
915913

916914
// Require that the traits involved in this upcast are **equal**;
917915
// only the **lifetime bound** is changed.
@@ -938,7 +936,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
938936
}
939937

940938
// `T` -> `Trait`
941-
(_, &ty::Dynamic(ref data, r, _repr)) => {
939+
(_, &ty::Dynamic(ref data, r, ty::Dyn)) => {
942940
let mut object_dids = data.auto_traits().chain(data.principal_def_id());
943941
if let Some(did) = object_dids.find(|did| !tcx.is_object_safe(*did)) {
944942
return Err(TraitNotObjectSafe(did));

compiler/rustc_traits/src/chalk/lowering.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
326326
)),
327327
})
328328
}
329-
ty::Dynamic(predicates, region, _repr) => chalk_ir::TyKind::Dyn(chalk_ir::DynTy {
329+
// FIXME(dyn-star): handle the dynamic kind (dyn or dyn*)
330+
ty::Dynamic(predicates, region, _kind) => chalk_ir::TyKind::Dyn(chalk_ir::DynTy {
330331
bounds: predicates.lower_into(interner),
331332
lifetime: region.lower_into(interner),
332333
}),

compiler/rustc_type_ir/src/sty.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,33 @@ use rustc_data_structures::stable_hasher::HashStable;
1919
use rustc_serialize::{Decodable, Decoder, Encodable};
2020

2121
/// Specifies how a trait object is represented.
22-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
22+
#[derive(
23+
Clone,
24+
Copy,
25+
PartialEq,
26+
Eq,
27+
PartialOrd,
28+
Ord,
29+
Hash,
30+
Debug,
31+
Encodable,
32+
Decodable,
33+
HashStable_Generic
34+
)]
2335
pub enum DynKind {
2436
/// An unsized `dyn Trait` object
2537
Dyn,
2638
/// A sized `dyn* Trait` object
39+
///
40+
/// These objects are represented as a `(data, vtable)` pair where `data` is a ptr-sized value
41+
/// (often a pointer to the real object, but not necessarily) and `vtable` is a pointer to
42+
/// the vtable for `dyn* Trait`. The representation is essentially the same as `&dyn Trait`
43+
/// or similar, but the drop function included in the vtable is responsible for freeing the
44+
/// underlying storage if needed. This allows a `dyn*` object to be treated agnostically with
45+
/// respect to whether it points to a `Box<T>`, `Rc<T>`, etc.
2746
DynStar,
2847
}
2948

30-
// Manually implemented because deriving HashStable requires rustc_query_system, which would
31-
// create a cyclic dependency.
32-
impl<CTX> HashStable<CTX> for DynKind {
33-
fn hash_stable(
34-
&self,
35-
hcx: &mut CTX,
36-
hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
37-
) {
38-
std::mem::discriminant(self).hash_stable(hcx, hasher);
39-
}
40-
}
41-
4249
/// Defines the kinds of types used by the type system.
4350
///
4451
/// Types written by the user start out as `hir::TyKind` and get

compiler/rustc_typeck/src/check/cast.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
105105

106106
Ok(match *t.kind() {
107107
ty::Slice(_) | ty::Str => Some(PointerKind::Length),
108-
ty::Dynamic(ref tty, ..) => Some(PointerKind::VTable(tty.principal_def_id())),
108+
ty::Dynamic(ref tty, _, ty::Dyn) => Some(PointerKind::VTable(tty.principal_def_id())),
109109
ty::Adt(def, substs) if def.is_struct() => match def.non_enum_variant().fields.last() {
110110
None => Some(PointerKind::Thin),
111111
Some(f) => {
@@ -142,6 +142,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
142142
| ty::Generator(..)
143143
| ty::Adt(..)
144144
| ty::Never
145+
| ty::Dynamic(_, _, ty::DynStar)
145146
| ty::Error(_) => {
146147
let reported = self
147148
.tcx
@@ -246,7 +247,7 @@ fn check_dyn_star_cast<'tcx>(
246247
let cause = ObligationCause::new(
247248
expr.span,
248249
fcx.body_id,
249-
// FIXME: Use a better obligation cause code
250+
// FIXME(dyn-star): Use a better obligation cause code
250251
ObligationCauseCode::MiscObligation,
251252
);
252253

@@ -927,10 +928,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
927928

928929
(Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
929930

930-
// FIXME: this needs more conditions...
931+
// FIXME(dyn-star): this needs more conditions...
931932
(_, DynStar) => Ok(CastKind::DynStarCast),
932933

933-
// FIXME: do we want to allow dyn* upcasting or other casts?
934+
// FIXME(dyn-star): do we want to allow dyn* upcasting or other casts?
934935
(DynStar, _) => Err(CastError::IllegalCast),
935936
}
936937
}

src/test/ui/dyn-star/const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-pass
22
#![feature(dyn_star)]
3-
#![allow(unused)]
3+
#![allow(unused, incomplete_features)]
44

55
use std::fmt::Debug;
66

src/test/ui/dyn-star/drop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-pass
22
// check-run-results
33
#![feature(dyn_star)]
4+
#![allow(incomplete_features)]
45

56
use std::fmt::Debug;
67

src/test/ui/dyn-star/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(dyn_star)]
2+
#![allow(incomplete_features)]
23

34
use std::fmt::Debug;
45

src/test/ui/dyn-star/error.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the trait bound `{integer}: Foo` is not satisfied
2-
--> $DIR/error.rs:9:27
2+
--> $DIR/error.rs:10:27
33
|
44
LL | let dyn_i: dyn* Foo = i as dyn* Foo;
55
| ^ the trait `Foo` is not implemented for `{integer}`

src/test/ui/dyn-star/make-dyn-star.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-pass
22
#![feature(dyn_star)]
3+
#![allow(incomplete_features)]
34

45
use std::fmt::Debug;
56

src/test/ui/dyn-star/method.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-pass
22
#![feature(dyn_star)]
3+
#![allow(incomplete_features)]
34

45
trait Foo {
56
fn get(&self) -> usize;

src/test/ui/dyn-star/syntax.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// check-pass
44

55
#![feature(dyn_star)]
6+
#![allow(incomplete_features)]
67

78
pub fn dyn_star_parameter(_: dyn* Send) {
89
}

0 commit comments

Comments
 (0)