Skip to content

Commit 0a601d6

Browse files
author
bors-servo
authored
Auto merge of #1293 - strake:use_associated_consts, r=emilio
optionally use associated constants in bitfields See #1166 r? @emilio
2 parents 0b4f5be + 7024cf6 commit 0a601d6

File tree

5 files changed

+103
-43
lines changed

5 files changed

+103
-43
lines changed

src/codegen/mod.rs

+26-14
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ impl EnumVariation {
21262126

21272127
fn is_bitfield(&self) -> bool {
21282128
match *self {
2129-
EnumVariation::Bitfield => true,
2129+
EnumVariation::Bitfield {..} => true,
21302130
_ => false
21312131
}
21322132
}
@@ -2247,6 +2247,7 @@ impl<'a> EnumBuilder<'a> {
22472247
mangling_prefix: Option<&str>,
22482248
rust_ty: quote::Tokens,
22492249
result: &mut CodegenResult<'b>,
2250+
is_ty_named: bool,
22502251
) -> Self {
22512252
let variant_name = ctx.rust_mangle(variant.name());
22522253
let expr = match variant.val() {
@@ -2278,19 +2279,28 @@ impl<'a> EnumBuilder<'a> {
22782279
}
22792280
}
22802281

2281-
EnumBuilder::Bitfield { .. } => {
2282-
let constant_name = match mangling_prefix {
2283-
Some(prefix) => {
2284-
Cow::Owned(format!("{}_{}", prefix, variant_name))
2285-
}
2286-
None => variant_name,
2287-
};
2288-
2289-
let ident = ctx.rust_ident(constant_name);
2290-
result.push(quote! {
2291-
#doc
2292-
pub const #ident : #rust_ty = #rust_ty ( #expr );
2293-
});
2282+
EnumBuilder::Bitfield { canonical_name, .. } => {
2283+
if ctx.options().rust_features().associated_const && is_ty_named {
2284+
let enum_ident = ctx.rust_ident(canonical_name);
2285+
let variant_ident = ctx.rust_ident(variant_name);
2286+
result.push(quote! {
2287+
impl #enum_ident {
2288+
#doc
2289+
pub const #variant_ident : #rust_ty = #rust_ty ( #expr );
2290+
}
2291+
});
2292+
} else {
2293+
let ident = ctx.rust_ident(match mangling_prefix {
2294+
Some(prefix) => {
2295+
Cow::Owned(format!("{}_{}", prefix, variant_name))
2296+
}
2297+
None => variant_name,
2298+
});
2299+
result.push(quote! {
2300+
#doc
2301+
pub const #ident : #rust_ty = #rust_ty ( #expr );
2302+
});
2303+
}
22942304

22952305
self
22962306
}
@@ -2625,6 +2635,7 @@ impl CodeGenerator for Enum {
26252635
constant_mangling_prefix,
26262636
enum_rust_ty.clone(),
26272637
result,
2638+
enum_ty.name().is_some(),
26282639
);
26292640
}
26302641
}
@@ -2635,6 +2646,7 @@ impl CodeGenerator for Enum {
26352646
constant_mangling_prefix,
26362647
enum_rust_ty.clone(),
26372648
result,
2649+
enum_ty.name().is_some(),
26382650
);
26392651

26402652
let variant_name = ctx.rust_ident(variant.name());

src/features.rs

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ macro_rules! rust_target_base {
9090
=> Stable_1_0 => 1.0;
9191
/// Rust stable 1.19
9292
=> Stable_1_19 => 1.19;
93+
/// Rust stable 1.20
94+
=> Stable_1_20 => 1.20;
9395
/// Rust stable 1.21
9496
=> Stable_1_21 => 1.21;
9597
/// Rust stable 1.25
@@ -142,6 +144,8 @@ rust_feature_def!(
142144
=> builtin_clone_impls;
143145
/// repr(align) https://github.com/rust-lang/rust/pull/47006
144146
=> repr_align;
147+
/// associated constants https://github.com/rust-lang/rust/issues/29646
148+
=> associated_const;
145149
);
146150

147151
impl From<RustTarget> for RustFeatures {
@@ -152,6 +156,10 @@ impl From<RustTarget> for RustFeatures {
152156
features.untagged_union = true;
153157
}
154158

159+
if rust_target >= RustTarget::Stable_1_20 {
160+
features.associated_const = true;
161+
}
162+
155163
if rust_target >= RustTarget::Stable_1_21 {
156164
features.builtin_clone_impls = true;
157165
}

tests/expectations/tests/bitfield-enum-basic.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
44

5-
pub const Foo_Bar: Foo = Foo(2);
6-
pub const Foo_Baz: Foo = Foo(4);
7-
pub const Foo_Duplicated: Foo = Foo(4);
8-
pub const Foo_Negative: Foo = Foo(-3);
5+
impl Foo {
6+
pub const Bar: Foo = Foo(2);
7+
}
8+
impl Foo {
9+
pub const Baz: Foo = Foo(4);
10+
}
11+
impl Foo {
12+
pub const Duplicated: Foo = Foo(4);
13+
}
14+
impl Foo {
15+
pub const Negative: Foo = Foo(-3);
16+
}
917
impl ::std::ops::BitOr<Foo> for Foo {
1018
type Output = Self;
1119
#[inline]
@@ -35,10 +43,18 @@ impl ::std::ops::BitAndAssign for Foo {
3543
#[repr(C)]
3644
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3745
pub struct Foo(pub i32);
38-
pub const Buz_Bar: Buz = Buz(2);
39-
pub const Buz_Baz: Buz = Buz(4);
40-
pub const Buz_Duplicated: Buz = Buz(4);
41-
pub const Buz_Negative: Buz = Buz(-3);
46+
impl Buz {
47+
pub const Bar: Buz = Buz(2);
48+
}
49+
impl Buz {
50+
pub const Baz: Buz = Buz(4);
51+
}
52+
impl Buz {
53+
pub const Duplicated: Buz = Buz(4);
54+
}
55+
impl Buz {
56+
pub const Negative: Buz = Buz(-3);
57+
}
4258
impl ::std::ops::BitOr<Buz> for Buz {
4359
type Output = Self;
4460
#[inline]

tests/expectations/tests/enum-doc-bitfield.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,33 @@
22

33
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
44

5-
/// Document field with three slashes
6-
pub const B_VAR_A: B = B(0);
7-
/// Document field with preceeding star
8-
pub const B_VAR_B: B = B(1);
9-
/// Document field with preceeding exclamation
10-
pub const B_VAR_C: B = B(2);
11-
/// < Document field with following star
12-
pub const B_VAR_D: B = B(3);
13-
/// < Document field with following exclamation
14-
pub const B_VAR_E: B = B(4);
15-
/// Document field with preceeding star, with a loong long multiline
16-
/// comment.
17-
///
18-
/// Very interesting documentation, definitely.
19-
pub const B_VAR_F: B = B(5);
5+
impl B {
6+
/// Document field with three slashes
7+
pub const VAR_A: B = B(0);
8+
}
9+
impl B {
10+
/// Document field with preceeding star
11+
pub const VAR_B: B = B(1);
12+
}
13+
impl B {
14+
/// Document field with preceeding exclamation
15+
pub const VAR_C: B = B(2);
16+
}
17+
impl B {
18+
/// < Document field with following star
19+
pub const VAR_D: B = B(3);
20+
}
21+
impl B {
22+
/// < Document field with following exclamation
23+
pub const VAR_E: B = B(4);
24+
}
25+
impl B {
26+
/// Document field with preceeding star, with a loong long multiline
27+
/// comment.
28+
///
29+
/// Very interesting documentation, definitely.
30+
pub const VAR_F: B = B(5);
31+
}
2032
impl ::std::ops::BitOr<B> for B {
2133
type Output = Self;
2234
#[inline]

tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
44

5-
pub const MyDupeEnum_A: MyDupeEnum = MyDupeEnum(0);
6-
pub const MyDupeEnum_A_alias: MyDupeEnum = MyDupeEnum(0);
7-
pub const MyDupeEnum_B: MyDupeEnum = MyDupeEnum(1);
5+
impl MyDupeEnum {
6+
pub const A: MyDupeEnum = MyDupeEnum(0);
7+
}
8+
impl MyDupeEnum {
9+
pub const A_alias: MyDupeEnum = MyDupeEnum(0);
10+
}
11+
impl MyDupeEnum {
12+
pub const B: MyDupeEnum = MyDupeEnum(1);
13+
}
814
impl ::std::ops::BitOr<MyDupeEnum> for MyDupeEnum {
915
type Output = Self;
1016
#[inline]
@@ -34,9 +40,15 @@ impl ::std::ops::BitAndAssign for MyDupeEnum {
3440
#[repr(C)]
3541
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3642
pub struct MyDupeEnum(pub u32);
37-
pub const MyOtherDupeEnum_C: MyOtherDupeEnum = MyOtherDupeEnum(0);
38-
pub const MyOtherDupeEnum_C_alias: MyOtherDupeEnum = MyOtherDupeEnum(0);
39-
pub const MyOtherDupeEnum_D: MyOtherDupeEnum = MyOtherDupeEnum(1);
43+
impl MyOtherDupeEnum {
44+
pub const C: MyOtherDupeEnum = MyOtherDupeEnum(0);
45+
}
46+
impl MyOtherDupeEnum {
47+
pub const C_alias: MyOtherDupeEnum = MyOtherDupeEnum(0);
48+
}
49+
impl MyOtherDupeEnum {
50+
pub const D: MyOtherDupeEnum = MyOtherDupeEnum(1);
51+
}
4052
impl ::std::ops::BitOr<MyOtherDupeEnum> for MyOtherDupeEnum {
4153
type Output = Self;
4254
#[inline]

0 commit comments

Comments
 (0)