Skip to content

Commit 0a6b941

Browse files
committed
Auto merge of #103572 - Dylan-DPC:rollup-a8bnxrw, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #95710 (Stabilize arbitrary_enum_discriminant, take 2) - #102706 (Support excluding the generation of the standalone docs) - #103428 (Removed verbose printing from the `PrettyPrinter` when printing constants) - #103543 (Update books) - #103546 (interpret: a bit of cast cleanup) - #103554 (rustdoc: add visible focus outline to rustdoc-toggle) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 43dd3d5 + 74a4c67 commit 0a6b941

37 files changed

+131
-285
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1-61
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast as ast;
22
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
33
use rustc_ast::{AssocConstraint, AssocConstraintKind, NodeId};
4-
use rustc_ast::{PatKind, RangeEnd, VariantData};
4+
use rustc_ast::{PatKind, RangeEnd};
55
use rustc_errors::{struct_span_err, Applicability, StashKey};
66
use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP};
77
use rustc_session::parse::{feature_err, feature_err_issue, feature_warn};
@@ -116,46 +116,6 @@ impl<'a> PostExpansionVisitor<'a> {
116116
}
117117
}
118118

119-
fn maybe_report_invalid_custom_discriminants(&self, variants: &[ast::Variant]) {
120-
let has_fields = variants.iter().any(|variant| match variant.data {
121-
VariantData::Tuple(..) | VariantData::Struct(..) => true,
122-
VariantData::Unit(..) => false,
123-
});
124-
125-
let discriminant_spans = variants
126-
.iter()
127-
.filter(|variant| match variant.data {
128-
VariantData::Tuple(..) | VariantData::Struct(..) => false,
129-
VariantData::Unit(..) => true,
130-
})
131-
.filter_map(|variant| variant.disr_expr.as_ref().map(|c| c.value.span))
132-
.collect::<Vec<_>>();
133-
134-
if !discriminant_spans.is_empty() && has_fields {
135-
let mut err = feature_err(
136-
&self.sess.parse_sess,
137-
sym::arbitrary_enum_discriminant,
138-
discriminant_spans.clone(),
139-
"custom discriminant values are not allowed in enums with tuple or struct variants",
140-
);
141-
for sp in discriminant_spans {
142-
err.span_label(sp, "disallowed custom discriminant");
143-
}
144-
for variant in variants.iter() {
145-
match &variant.data {
146-
VariantData::Struct(..) => {
147-
err.span_label(variant.span, "struct variant defined here");
148-
}
149-
VariantData::Tuple(..) => {
150-
err.span_label(variant.span, "tuple variant defined here");
151-
}
152-
VariantData::Unit(..) => {}
153-
}
154-
}
155-
err.emit();
156-
}
157-
}
158-
159119
/// Feature gate `impl Trait` inside `type Alias = $type_expr;`.
160120
fn check_impl_trait(&self, ty: &ast::Ty) {
161121
struct ImplTraitVisitor<'a> {
@@ -273,26 +233,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
273233
}
274234
}
275235

276-
ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => {
277-
for variant in variants {
278-
match (&variant.data, &variant.disr_expr) {
279-
(ast::VariantData::Unit(..), _) => {}
280-
(_, Some(disr_expr)) => gate_feature_post!(
281-
&self,
282-
arbitrary_enum_discriminant,
283-
disr_expr.value.span,
284-
"discriminants on non-unit variants are experimental"
285-
),
286-
_ => {}
287-
}
288-
}
289-
290-
let has_feature = self.features.arbitrary_enum_discriminant;
291-
if !has_feature && !i.span.allows_unstable(sym::arbitrary_enum_discriminant) {
292-
self.maybe_report_invalid_custom_discriminants(&variants);
293-
}
294-
}
295-
296236
ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, ref of_trait, .. }) => {
297237
if let ast::ImplPolarity::Negative(span) = polarity {
298238
gate_feature_post!(

compiler/rustc_const_eval/src/interpret/cast.rs

+30-31
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,19 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
138138
Ok(())
139139
}
140140

141+
/// Handles 'IntToInt' and 'IntToFloat' casts.
141142
pub fn int_to_int_or_float(
142143
&self,
143144
src: &ImmTy<'tcx, M::Provenance>,
144145
cast_ty: Ty<'tcx>,
145146
) -> InterpResult<'tcx, Immediate<M::Provenance>> {
146-
if (src.layout.ty.is_integral() || src.layout.ty.is_char() || src.layout.ty.is_bool())
147-
&& (cast_ty.is_floating_point() || cast_ty.is_integral() || cast_ty.is_char())
148-
{
149-
let scalar = src.to_scalar();
150-
Ok(self.cast_from_int_like(scalar, src.layout, cast_ty)?.into())
151-
} else {
152-
bug!("Unexpected cast from type {:?}", src.layout.ty)
153-
}
147+
assert!(src.layout.ty.is_integral() || src.layout.ty.is_char() || src.layout.ty.is_bool());
148+
assert!(cast_ty.is_floating_point() || cast_ty.is_integral() || cast_ty.is_char());
149+
150+
Ok(self.cast_from_int_like(src.to_scalar(), src.layout, cast_ty)?.into())
154151
}
155152

153+
/// Handles 'FloatToFloat' and 'FloatToInt' casts.
156154
pub fn float_to_float_or_int(
157155
&self,
158156
src: &ImmTy<'tcx, M::Provenance>,
@@ -180,31 +178,29 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
180178
src: &ImmTy<'tcx, M::Provenance>,
181179
cast_ty: Ty<'tcx>,
182180
) -> InterpResult<'tcx, Immediate<M::Provenance>> {
181+
assert!(src.layout.ty.is_any_ptr());
182+
assert!(cast_ty.is_unsafe_ptr());
183183
// Handle casting any ptr to raw ptr (might be a fat ptr).
184-
if src.layout.ty.is_any_ptr() && cast_ty.is_unsafe_ptr() {
185-
let dest_layout = self.layout_of(cast_ty)?;
186-
if dest_layout.size == src.layout.size {
187-
// Thin or fat pointer that just hast the ptr kind of target type changed.
188-
return Ok(**src);
189-
} else {
190-
// Casting the metadata away from a fat ptr.
191-
assert_eq!(src.layout.size, 2 * self.pointer_size());
192-
assert_eq!(dest_layout.size, self.pointer_size());
193-
assert!(src.layout.ty.is_unsafe_ptr());
194-
return match **src {
195-
Immediate::ScalarPair(data, _) => Ok(data.into()),
196-
Immediate::Scalar(..) => span_bug!(
197-
self.cur_span(),
198-
"{:?} input to a fat-to-thin cast ({:?} -> {:?})",
199-
*src,
200-
src.layout.ty,
201-
cast_ty
202-
),
203-
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
204-
};
205-
}
184+
let dest_layout = self.layout_of(cast_ty)?;
185+
if dest_layout.size == src.layout.size {
186+
// Thin or fat pointer that just hast the ptr kind of target type changed.
187+
return Ok(**src);
206188
} else {
207-
bug!("Can't cast 'Ptr' or 'FnPtr' into {:?}", cast_ty);
189+
// Casting the metadata away from a fat ptr.
190+
assert_eq!(src.layout.size, 2 * self.pointer_size());
191+
assert_eq!(dest_layout.size, self.pointer_size());
192+
assert!(src.layout.ty.is_unsafe_ptr());
193+
return match **src {
194+
Immediate::ScalarPair(data, _) => Ok(data.into()),
195+
Immediate::Scalar(..) => span_bug!(
196+
self.cur_span(),
197+
"{:?} input to a fat-to-thin cast ({:?} -> {:?})",
198+
*src,
199+
src.layout.ty,
200+
cast_ty
201+
),
202+
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
203+
};
208204
}
209205
}
210206

@@ -243,6 +239,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
243239
Ok(Scalar::from_maybe_pointer(ptr, self).into())
244240
}
245241

242+
/// Low-level cast helper function. This works directly on scalars and can take 'int-like' input
243+
/// type (basically everything with a scalar layout) to int/float/char types.
246244
pub fn cast_from_int_like(
247245
&self,
248246
scalar: Scalar<M::Provenance>, // input value (there is no ScalarTy so we separate data+layout)
@@ -282,6 +280,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
282280
})
283281
}
284282

283+
/// Low-level cast helper function. Converts an apfloat `f` into int or float types.
285284
fn cast_from_float<F>(&self, f: F, dest_ty: Ty<'tcx>) -> Scalar<M::Provenance>
286285
where
287286
F: Float + Into<Scalar<M::Provenance>> + FloatConvert<Single> + FloatConvert<Double>,

compiler/rustc_const_eval/src/interpret/intrinsics/type_name.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir::definitions::DisambiguatedDefPathData;
44
use rustc_middle::mir::interpret::{Allocation, ConstAllocation};
55
use rustc_middle::ty::{
66
self,
7-
print::{PrettyPrinter, Print, Printer},
7+
print::{with_no_verbose_constants, PrettyPrinter, Print, Printer},
88
subst::{GenericArg, GenericArgKind},
99
Ty, TyCtxt,
1010
};
@@ -190,7 +190,9 @@ impl Write for AbsolutePathPrinter<'_> {
190190

191191
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
192192
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
193-
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
193+
let path = with_no_verbose_constants!(
194+
AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path
195+
);
194196
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
195197
tcx.intern_const_alloc(alloc)
196198
}

compiler/rustc_error_codes/src/error_codes/E0732.md

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ An `enum` with a discriminant must specify a `#[repr(inttype)]`.
33
Erroneous code example:
44

55
```compile_fail,E0732
6-
#![feature(arbitrary_enum_discriminant)]
7-
86
enum Enum { // error!
97
Unit = 1,
108
Tuple() = 2,
@@ -20,8 +18,6 @@ is a well-defined way to extract a variant's discriminant from a value;
2018
for instance:
2119

2220
```
23-
#![feature(arbitrary_enum_discriminant)]
24-
2521
#[repr(u8)]
2622
enum Enum {
2723
Unit = 3,

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ declare_features! (
5353
(accepted, abi_sysv64, "1.24.0", Some(36167), None),
5454
/// Allows using ADX intrinsics from `core::arch::{x86, x86_64}`.
5555
(accepted, adx_target_feature, "1.61.0", Some(44839), None),
56+
/// Allows explicit discriminants on non-unit enum variants.
57+
(accepted, arbitrary_enum_discriminant, "CURRENT_RUSTC_VERSION", Some(60553), None),
5658
/// Allows using `sym` operands in inline assembly.
5759
(accepted, asm_sym, "CURRENT_RUSTC_VERSION", Some(93333), None),
5860
/// Allows the definition of associated constants in `trait` or `impl` blocks.

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ declare_features! (
292292
(incomplete, adt_const_params, "1.56.0", Some(95174), None),
293293
/// Allows defining an `#[alloc_error_handler]`.
294294
(active, alloc_error_handler, "1.29.0", Some(51540), None),
295-
/// Allows explicit discriminants on non-unit enum variants.
296-
(active, arbitrary_enum_discriminant, "1.37.0", Some(60553), None),
297295
/// Allows trait methods with arbitrary self types.
298296
(active, arbitrary_self_types, "1.23.0", Some(44874), None),
299297
/// Allows using `const` operands in inline assembly.

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, vs: &'tcx [hir::Variant<'tcx>], def_id: L
11801180
}
11811181
}
11821182

1183-
if tcx.adt_def(def_id).repr().int.is_none() && tcx.features().arbitrary_enum_discriminant {
1183+
if tcx.adt_def(def_id).repr().int.is_none() {
11841184
let is_unit = |var: &hir::Variant<'_>| matches!(var.data, hir::VariantData::Unit(..));
11851185

11861186
let has_disr = |var: &hir::Variant<'_>| var.disr_expr.is_some();

compiler/rustc_middle/src/ty/print/pretty.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ thread_local! {
6363
static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
6464
static NO_QUERIES: Cell<bool> = const { Cell::new(false) };
6565
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
66+
static NO_VERBOSE_CONSTANTS: Cell<bool> = const { Cell::new(false) };
6667
}
6768

6869
macro_rules! define_helper {
@@ -117,6 +118,9 @@ define_helper!(
117118
/// Prevent selection of visible paths. `Display` impl of DefId will prefer
118119
/// visible (public) reexports of types as paths.
119120
fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
121+
/// Prevent verbose printing of constants. Verbose printing of constants is
122+
/// never desirable in some contexts like `std::any::type_name`.
123+
fn with_no_verbose_constants(NoVerboseConstantsGuard, NO_VERBOSE_CONSTANTS);
120124
);
121125

122126
/// The "region highlights" are used to control region printing during
@@ -759,7 +763,7 @@ pub trait PrettyPrinter<'tcx>:
759763
}
760764
ty::Array(ty, sz) => {
761765
p!("[", print(ty), "; ");
762-
if self.tcx().sess.verbose() {
766+
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
763767
p!(write("{:?}", sz));
764768
} else if let ty::ConstKind::Unevaluated(..) = sz.kind() {
765769
// Do not try to evaluate unevaluated constants. If we are const evaluating an
@@ -1181,7 +1185,7 @@ pub trait PrettyPrinter<'tcx>:
11811185
) -> Result<Self::Const, Self::Error> {
11821186
define_scoped_cx!(self);
11831187

1184-
if self.tcx().sess.verbose() {
1188+
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
11851189
p!(write("Const({:?}: {:?})", ct.kind(), ct.ty()));
11861190
return Ok(self);
11871191
}
@@ -1416,7 +1420,7 @@ pub trait PrettyPrinter<'tcx>:
14161420
) -> Result<Self::Const, Self::Error> {
14171421
define_scoped_cx!(self);
14181422

1419-
if self.tcx().sess.verbose() {
1423+
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
14201424
p!(write("ValTree({:?}: ", valtree), print(ty), ")");
14211425
return Ok(self);
14221426
}

0 commit comments

Comments
 (0)