Skip to content

Commit ec3ac1d

Browse files
committed
builtin-derive: tag → discriminant
1 parent f3c6608 commit ec3ac1d

File tree

6 files changed

+107
-107
lines changed

6 files changed

+107
-107
lines changed

compiler/rustc_builtin_macros/src/deriving/clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ fn cs_clone(
181181
all_fields = af;
182182
vdata = &variant.data;
183183
}
184-
EnumTag(..) | AllFieldlessEnum(..) => {
185-
cx.dcx().span_bug(trait_span, format!("enum tags in `derive({name})`",))
184+
EnumDiscr(..) | AllFieldlessEnum(..) => {
185+
cx.dcx().span_bug(trait_span, format!("enum discriminants in `derive({name})`",))
186186
}
187187
StaticEnum(..) | StaticStruct(..) => {
188188
cx.dcx().span_bug(trait_span, format!("associated function in `derive({name})`"))

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ pub fn expand_deriving_partial_ord(
2020
Path(Path::new_(pathvec_std!(option::Option), vec![Box::new(ordering_ty)], PathKind::Std));
2121

2222
// Order in which to perform matching
23-
let tag_then_data = if let Annotatable::Item(item) = item
23+
let discr_then_data = if let Annotatable::Item(item) = item
2424
&& let ItemKind::Enum(def, _) = &item.kind
2525
{
2626
let dataful: Vec<bool> = def.variants.iter().map(|v| !v.data.fields().is_empty()).collect();
2727
match dataful.iter().filter(|&&b| b).count() {
28-
// No data, placing the tag check first makes codegen simpler
28+
// No data, placing the discriminant check first makes codegen simpler
2929
0 => true,
3030
1..=2 => false,
3131
_ => (0..dataful.len() - 1).any(|i| {
@@ -50,7 +50,7 @@ pub fn expand_deriving_partial_ord(
5050
attributes: thin_vec![cx.attr_word(sym::inline, span)],
5151
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
5252
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
53-
cs_partial_cmp(cx, span, substr, tag_then_data)
53+
cs_partial_cmp(cx, span, substr, discr_then_data)
5454
})),
5555
};
5656

@@ -72,7 +72,7 @@ fn cs_partial_cmp(
7272
cx: &ExtCtxt<'_>,
7373
span: Span,
7474
substr: &Substructure<'_>,
75-
tag_then_data: bool,
75+
discr_then_data: bool,
7676
) -> BlockOrExpr {
7777
let test_id = Ident::new(sym::cmp, span);
7878
let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
@@ -108,12 +108,12 @@ fn cs_partial_cmp(
108108
// cmp => cmp
109109
// }
110110
// ```
111-
// where `expr2` is `partial_cmp(self_tag, other_tag)`, and `expr1` is a `match`
112-
// against the enum variants. This means that we begin by comparing the enum tags,
111+
// where `expr2` is `partial_cmp(self_discr, other_discr)`, and `expr1` is a `match`
112+
// against the enum variants. This means that we begin by comparing the enum discriminants,
113113
// before either inspecting their contents (if they match), or returning
114-
// the `cmp::Ordering` of comparing the enum tags.
114+
// the `cmp::Ordering` of comparing the enum discriminants.
115115
// ```
116-
// match partial_cmp(self_tag, other_tag) {
116+
// match partial_cmp(self_discr, other_discr) {
117117
// Some(Ordering::Equal) => match (self, other) {
118118
// (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
119119
// (Self::B(self_0), Self::B(other_0)) => partial_cmp(self_0, other_0),
@@ -126,12 +126,12 @@ fn cs_partial_cmp(
126126
// ```
127127
// match (self, other) {
128128
// (Self::A(self_0), Self::A(other_0)) => partial_cmp(self_0, other_0),
129-
// _ => partial_cmp(self_tag, other_tag)
129+
// _ => partial_cmp(self_discr, other_discr)
130130
// }
131131
// ```
132132
// Reference: https://github.com/rust-lang/rust/pull/103659#issuecomment-1328126354
133133

134-
if !tag_then_data
134+
if !discr_then_data
135135
&& let ExprKind::Match(_, arms, _) = &mut expr1.kind
136136
&& let Some(last) = arms.last_mut()
137137
&& let PatKind::Wild = last.pat.kind

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) ->
5353
Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
5454
EnumMatching(_, v, fields) => (v.ident, &v.data, fields),
5555
AllFieldlessEnum(enum_def) => return show_fieldless_enum(cx, span, enum_def, substr),
56-
EnumTag(..) | StaticStruct(..) | StaticEnum(..) => {
56+
EnumDiscr(..) | StaticStruct(..) | StaticEnum(..) => {
5757
cx.dcx().span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
5858
}
5959
};

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+44-44
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! `struct T(i32, char)`).
2222
//! - `EnumMatching`, when `Self` is an enum and all the arguments are the
2323
//! same variant of the enum (e.g., `Some(1)`, `Some(3)` and `Some(4)`)
24-
//! - `EnumTag` when `Self` is an enum, for comparing the enum tags.
24+
//! - `EnumDiscr` when `Self` is an enum, for comparing the enum discriminants.
2525
//! - `StaticEnum` and `StaticStruct` for static methods, where the type
2626
//! being derived upon is either an enum or struct respectively. (Any
2727
//! argument with type Self is just grouped among the non-self
@@ -143,11 +143,11 @@
143143
//! )
144144
//! ```
145145
//!
146-
//! For the tags,
146+
//! For the discriminants,
147147
//!
148148
//! ```text
149-
//! EnumTag(
150-
//! &[<ident of self tag>, <ident of other tag>],
149+
//! EnumDiscr(
150+
//! &[<ident of self discriminant>, <ident of other discriminant>],
151151
//! <expr to combine with>,
152152
//! )
153153
//! ```
@@ -315,10 +315,10 @@ pub enum SubstructureFields<'a> {
315315
/// variant.
316316
EnumMatching(usize, &'a ast::Variant, Vec<FieldInfo>),
317317

318-
/// The tag of an enum. The first field is a `FieldInfo` for the tags, as
318+
/// The discriminant of an enum. The first field is a `FieldInfo` for the discriminants, as
319319
/// if they were fields. The second field is the expression to combine the
320-
/// tag expression with; it will be `None` if no match is necessary.
321-
EnumTag(FieldInfo, Option<P<Expr>>),
320+
/// discriminant expression with; it will be `None` if no match is necessary.
321+
EnumDiscr(FieldInfo, Option<P<Expr>>),
322322

323323
/// A static method where `Self` is a struct.
324324
StaticStruct(&'a ast::VariantData, StaticFields),
@@ -1137,9 +1137,9 @@ impl<'a> MethodDef<'a> {
11371137
/// impl ::core::cmp::PartialEq for A {
11381138
/// #[inline]
11391139
/// fn eq(&self, other: &A) -> bool {
1140-
/// let __self_tag = ::core::intrinsics::discriminant_value(self);
1141-
/// let __arg1_tag = ::core::intrinsics::discriminant_value(other);
1142-
/// __self_tag == __arg1_tag
1140+
/// let __self_discr = ::core::intrinsics::discriminant_value(self);
1141+
/// let __arg1_discr = ::core::intrinsics::discriminant_value(other);
1142+
/// __self_discr == __arg1_discr
11431143
/// && match (self, other) {
11441144
/// (A::A2(__self_0), A::A2(__arg1_0)) => *__self_0 == *__arg1_0,
11451145
/// _ => true,
@@ -1148,7 +1148,7 @@ impl<'a> MethodDef<'a> {
11481148
/// }
11491149
/// ```
11501150
///
1151-
/// Creates a tag check combined with a match for a tuple of all
1151+
/// Creates a discriminant check combined with a match for a tuple of all
11521152
/// `selflike_args`, with an arm for each variant with fields, possibly an
11531153
/// arm for each fieldless variant (if `unify_fieldless_variants` is not
11541154
/// `Unify`), and possibly a default arm.
@@ -1169,7 +1169,7 @@ impl<'a> MethodDef<'a> {
11691169
let span = trait_.span;
11701170
let variants = &enum_def.variants;
11711171

1172-
// Traits that unify fieldless variants always use the tag(s).
1172+
// Traits that unify fieldless variants always use the discriminant(s).
11731173
let unify_fieldless_variants =
11741174
self.fieldless_variants_strategy == FieldlessVariantsStrategy::Unify;
11751175

@@ -1199,25 +1199,25 @@ impl<'a> MethodDef<'a> {
11991199
//
12001200
// e.g. for `PartialEq::eq` builds two statements:
12011201
// ```
1202-
// let __self_tag = ::core::intrinsics::discriminant_value(self);
1203-
// let __arg1_tag = ::core::intrinsics::discriminant_value(other);
1202+
// let __self_discr = ::core::intrinsics::discriminant_value(self);
1203+
// let __arg1_discr = ::core::intrinsics::discriminant_value(other);
12041204
// ```
1205-
let get_tag_pieces = |cx: &ExtCtxt<'_>| {
1206-
let tag_idents: Vec<_> = prefixes
1205+
let get_discr_pieces = |cx: &ExtCtxt<'_>| {
1206+
let discr_idents: Vec<_> = prefixes
12071207
.iter()
1208-
.map(|name| Ident::from_str_and_span(&format!("{name}_tag"), span))
1208+
.map(|name| Ident::from_str_and_span(&format!("{name}_discr"), span))
12091209
.collect();
12101210

1211-
let mut tag_exprs: Vec<_> = tag_idents
1211+
let mut discr_exprs: Vec<_> = discr_idents
12121212
.iter()
12131213
.map(|&ident| cx.expr_addr_of(span, cx.expr_ident(span, ident)))
12141214
.collect();
12151215

1216-
let self_expr = tag_exprs.remove(0);
1217-
let other_selflike_exprs = tag_exprs;
1218-
let tag_field = FieldInfo { span, name: None, self_expr, other_selflike_exprs };
1216+
let self_expr = discr_exprs.remove(0);
1217+
let other_selflike_exprs = discr_exprs;
1218+
let discr_field = FieldInfo { span, name: None, self_expr, other_selflike_exprs };
12191219

1220-
let tag_let_stmts: ThinVec<_> = iter::zip(&tag_idents, &selflike_args)
1220+
let discr_let_stmts: ThinVec<_> = iter::zip(&discr_idents, &selflike_args)
12211221
.map(|(&ident, selflike_arg)| {
12221222
let variant_value = deriving::call_intrinsic(
12231223
cx,
@@ -1229,7 +1229,7 @@ impl<'a> MethodDef<'a> {
12291229
})
12301230
.collect();
12311231

1232-
(tag_field, tag_let_stmts)
1232+
(discr_field, discr_let_stmts)
12331233
};
12341234

12351235
// There are some special cases involving fieldless enums where no
@@ -1239,19 +1239,19 @@ impl<'a> MethodDef<'a> {
12391239
if variants.len() > 1 {
12401240
match self.fieldless_variants_strategy {
12411241
FieldlessVariantsStrategy::Unify => {
1242-
// If the type is fieldless and the trait uses the tag and
1242+
// If the type is fieldless and the trait uses the discriminant and
12431243
// there are multiple variants, we need just an operation on
1244-
// the tag(s).
1245-
let (tag_field, mut tag_let_stmts) = get_tag_pieces(cx);
1246-
let mut tag_check = self.call_substructure_method(
1244+
// the discriminant(s).
1245+
let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
1246+
let mut discr_check = self.call_substructure_method(
12471247
cx,
12481248
trait_,
12491249
type_ident,
12501250
nonselflike_args,
1251-
&EnumTag(tag_field, None),
1251+
&EnumDiscr(discr_field, None),
12521252
);
1253-
tag_let_stmts.append(&mut tag_check.0);
1254-
return BlockOrExpr(tag_let_stmts, tag_check.1);
1253+
discr_let_stmts.append(&mut discr_check.0);
1254+
return BlockOrExpr(discr_let_stmts, discr_check.1);
12551255
}
12561256
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless => {
12571257
return self.call_substructure_method(
@@ -1266,7 +1266,7 @@ impl<'a> MethodDef<'a> {
12661266
}
12671267
} else if variants.len() == 1 {
12681268
// If there is a single variant, we don't need an operation on
1269-
// the tag(s). Just use the most degenerate result.
1269+
// the discriminant(s). Just use the most degenerate result.
12701270
return self.call_substructure_method(
12711271
cx,
12721272
trait_,
@@ -1380,22 +1380,22 @@ impl<'a> MethodDef<'a> {
13801380
cx.expr_match(span, match_arg, match_arms)
13811381
};
13821382

1383-
// If the trait uses the tag and there are multiple variants, we need
1384-
// to add a tag check operation before the match. Otherwise, the match
1383+
// If the trait uses the discriminant and there are multiple variants, we need
1384+
// to add a discriminant check operation before the match. Otherwise, the match
13851385
// is enough.
13861386
if unify_fieldless_variants && variants.len() > 1 {
1387-
let (tag_field, mut tag_let_stmts) = get_tag_pieces(cx);
1387+
let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
13881388

1389-
// Combine a tag check with the match.
1390-
let mut tag_check_plus_match = self.call_substructure_method(
1389+
// Combine a discriminant check with the match.
1390+
let mut discr_check_plus_match = self.call_substructure_method(
13911391
cx,
13921392
trait_,
13931393
type_ident,
13941394
nonselflike_args,
1395-
&EnumTag(tag_field, Some(get_match_expr(selflike_args))),
1395+
&EnumDiscr(discr_field, Some(get_match_expr(selflike_args))),
13961396
);
1397-
tag_let_stmts.append(&mut tag_check_plus_match.0);
1398-
BlockOrExpr(tag_let_stmts, tag_check_plus_match.1)
1397+
discr_let_stmts.append(&mut discr_check_plus_match.0);
1398+
BlockOrExpr(discr_let_stmts, discr_check_plus_match.1)
13991399
} else {
14001400
BlockOrExpr(ThinVec::new(), Some(get_match_expr(selflike_args)))
14011401
}
@@ -1701,16 +1701,16 @@ where
17011701
rest.iter().rfold(base_expr, op)
17021702
}
17031703
}
1704-
EnumTag(tag_field, match_expr) => {
1705-
let tag_check_expr = f(cx, CsFold::Single(tag_field));
1704+
EnumDiscr(discr_field, match_expr) => {
1705+
let discr_check_expr = f(cx, CsFold::Single(discr_field));
17061706
if let Some(match_expr) = match_expr {
17071707
if use_foldl {
1708-
f(cx, CsFold::Combine(trait_span, tag_check_expr, match_expr.clone()))
1708+
f(cx, CsFold::Combine(trait_span, discr_check_expr, match_expr.clone()))
17091709
} else {
1710-
f(cx, CsFold::Combine(trait_span, match_expr.clone(), tag_check_expr))
1710+
f(cx, CsFold::Combine(trait_span, match_expr.clone(), discr_check_expr))
17111711
}
17121712
} else {
1713-
tag_check_expr
1713+
discr_check_expr
17141714
}
17151715
}
17161716
StaticEnum(..) | StaticStruct(..) => {

compiler/rustc_builtin_macros/src/deriving/hash.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ fn hash_substructure(cx: &ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'
6666
fields.iter().map(|field| call_hash(field.span, field.self_expr.clone())).collect();
6767
(stmts, None)
6868
}
69-
EnumTag(tag_field, match_expr) => {
70-
assert!(tag_field.other_selflike_exprs.is_empty());
71-
let stmts = thin_vec![call_hash(tag_field.span, tag_field.self_expr.clone())];
69+
EnumDiscr(discr_field, match_expr) => {
70+
assert!(discr_field.other_selflike_exprs.is_empty());
71+
let stmts = thin_vec![call_hash(discr_field.span, discr_field.self_expr.clone())];
7272
(stmts, match_expr.clone())
7373
}
7474
_ => cx.dcx().span_bug(trait_span, "impossible substructure in `derive(Hash)`"),

0 commit comments

Comments
 (0)