summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-04-07 08:56:20 -0400
committerGitHub <noreply@github.com>2018-04-07 08:56:20 -0400
commit0a601d6b3cd9fc32e119db2c33961d8f4db7e89e (patch)
treeb8b50e1e7cffeb1f77bf10ef2e2f9257ce60a90d
parent0b4f5be312cf4ccc5b382a612180ed58a4d10983 (diff)
parent7024cf682a6ff63bbafd63a6b570826ce422e2d4 (diff)
Auto merge of #1293 - strake:use_associated_consts, r=emilio
optionally use associated constants in bitfields See #1166 r? @emilio
-rw-r--r--src/codegen/mod.rs40
-rw-r--r--src/features.rs8
-rw-r--r--tests/expectations/tests/bitfield-enum-basic.rs32
-rw-r--r--tests/expectations/tests/enum-doc-bitfield.rs42
-rw-r--r--tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs24
5 files changed, 103 insertions, 43 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 39babb6b..b30da19b 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -2126,7 +2126,7 @@ impl EnumVariation {
fn is_bitfield(&self) -> bool {
match *self {
- EnumVariation::Bitfield => true,
+ EnumVariation::Bitfield {..} => true,
_ => false
}
}
@@ -2247,6 +2247,7 @@ impl<'a> EnumBuilder<'a> {
mangling_prefix: Option<&str>,
rust_ty: quote::Tokens,
result: &mut CodegenResult<'b>,
+ is_ty_named: bool,
) -> Self {
let variant_name = ctx.rust_mangle(variant.name());
let expr = match variant.val() {
@@ -2278,19 +2279,28 @@ impl<'a> EnumBuilder<'a> {
}
}
- EnumBuilder::Bitfield { .. } => {
- let constant_name = match mangling_prefix {
- Some(prefix) => {
- Cow::Owned(format!("{}_{}", prefix, variant_name))
- }
- None => variant_name,
- };
-
- let ident = ctx.rust_ident(constant_name);
- result.push(quote! {
- #doc
- pub const #ident : #rust_ty = #rust_ty ( #expr );
- });
+ EnumBuilder::Bitfield { canonical_name, .. } => {
+ if ctx.options().rust_features().associated_const && is_ty_named {
+ let enum_ident = ctx.rust_ident(canonical_name);
+ let variant_ident = ctx.rust_ident(variant_name);
+ result.push(quote! {
+ impl #enum_ident {
+ #doc
+ pub const #variant_ident : #rust_ty = #rust_ty ( #expr );
+ }
+ });
+ } else {
+ let ident = ctx.rust_ident(match mangling_prefix {
+ Some(prefix) => {
+ Cow::Owned(format!("{}_{}", prefix, variant_name))
+ }
+ None => variant_name,
+ });
+ result.push(quote! {
+ #doc
+ pub const #ident : #rust_ty = #rust_ty ( #expr );
+ });
+ }
self
}
@@ -2625,6 +2635,7 @@ impl CodeGenerator for Enum {
constant_mangling_prefix,
enum_rust_ty.clone(),
result,
+ enum_ty.name().is_some(),
);
}
}
@@ -2635,6 +2646,7 @@ impl CodeGenerator for Enum {
constant_mangling_prefix,
enum_rust_ty.clone(),
result,
+ enum_ty.name().is_some(),
);
let variant_name = ctx.rust_ident(variant.name());
diff --git a/src/features.rs b/src/features.rs
index 866d7126..93ebbc34 100644
--- a/src/features.rs
+++ b/src/features.rs
@@ -90,6 +90,8 @@ macro_rules! rust_target_base {
=> Stable_1_0 => 1.0;
/// Rust stable 1.19
=> Stable_1_19 => 1.19;
+ /// Rust stable 1.20
+ => Stable_1_20 => 1.20;
/// Rust stable 1.21
=> Stable_1_21 => 1.21;
/// Rust stable 1.25
@@ -142,6 +144,8 @@ rust_feature_def!(
=> builtin_clone_impls;
/// repr(align) https://github.com/rust-lang/rust/pull/47006
=> repr_align;
+ /// associated constants https://github.com/rust-lang/rust/issues/29646
+ => associated_const;
);
impl From<RustTarget> for RustFeatures {
@@ -152,6 +156,10 @@ impl From<RustTarget> for RustFeatures {
features.untagged_union = true;
}
+ if rust_target >= RustTarget::Stable_1_20 {
+ features.associated_const = true;
+ }
+
if rust_target >= RustTarget::Stable_1_21 {
features.builtin_clone_impls = true;
}
diff --git a/tests/expectations/tests/bitfield-enum-basic.rs b/tests/expectations/tests/bitfield-enum-basic.rs
index 091dc70b..a8140b2e 100644
--- a/tests/expectations/tests/bitfield-enum-basic.rs
+++ b/tests/expectations/tests/bitfield-enum-basic.rs
@@ -2,10 +2,18 @@
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
-pub const Foo_Bar: Foo = Foo(2);
-pub const Foo_Baz: Foo = Foo(4);
-pub const Foo_Duplicated: Foo = Foo(4);
-pub const Foo_Negative: Foo = Foo(-3);
+impl Foo {
+ pub const Bar: Foo = Foo(2);
+}
+impl Foo {
+ pub const Baz: Foo = Foo(4);
+}
+impl Foo {
+ pub const Duplicated: Foo = Foo(4);
+}
+impl Foo {
+ pub const Negative: Foo = Foo(-3);
+}
impl ::std::ops::BitOr<Foo> for Foo {
type Output = Self;
#[inline]
@@ -35,10 +43,18 @@ impl ::std::ops::BitAndAssign for Foo {
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Foo(pub i32);
-pub const Buz_Bar: Buz = Buz(2);
-pub const Buz_Baz: Buz = Buz(4);
-pub const Buz_Duplicated: Buz = Buz(4);
-pub const Buz_Negative: Buz = Buz(-3);
+impl Buz {
+ pub const Bar: Buz = Buz(2);
+}
+impl Buz {
+ pub const Baz: Buz = Buz(4);
+}
+impl Buz {
+ pub const Duplicated: Buz = Buz(4);
+}
+impl Buz {
+ pub const Negative: Buz = Buz(-3);
+}
impl ::std::ops::BitOr<Buz> for Buz {
type Output = Self;
#[inline]
diff --git a/tests/expectations/tests/enum-doc-bitfield.rs b/tests/expectations/tests/enum-doc-bitfield.rs
index 081b39ec..93d1f5f0 100644
--- a/tests/expectations/tests/enum-doc-bitfield.rs
+++ b/tests/expectations/tests/enum-doc-bitfield.rs
@@ -2,21 +2,33 @@
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
-/// Document field with three slashes
-pub const B_VAR_A: B = B(0);
-/// Document field with preceeding star
-pub const B_VAR_B: B = B(1);
-/// Document field with preceeding exclamation
-pub const B_VAR_C: B = B(2);
-/// < Document field with following star
-pub const B_VAR_D: B = B(3);
-/// < Document field with following exclamation
-pub const B_VAR_E: B = B(4);
-/// Document field with preceeding star, with a loong long multiline
-/// comment.
-///
-/// Very interesting documentation, definitely.
-pub const B_VAR_F: B = B(5);
+impl B {
+ /// Document field with three slashes
+ pub const VAR_A: B = B(0);
+}
+impl B {
+ /// Document field with preceeding star
+ pub const VAR_B: B = B(1);
+}
+impl B {
+ /// Document field with preceeding exclamation
+ pub const VAR_C: B = B(2);
+}
+impl B {
+ /// < Document field with following star
+ pub const VAR_D: B = B(3);
+}
+impl B {
+ /// < Document field with following exclamation
+ pub const VAR_E: B = B(4);
+}
+impl B {
+ /// Document field with preceeding star, with a loong long multiline
+ /// comment.
+ ///
+ /// Very interesting documentation, definitely.
+ pub const VAR_F: B = B(5);
+}
impl ::std::ops::BitOr<B> for B {
type Output = Self;
#[inline]
diff --git a/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs b/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
index aa836077..8c70625e 100644
--- a/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
+++ b/tests/expectations/tests/issue-1198-alias-rust-bitfield-enum.rs
@@ -2,9 +2,15 @@
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
-pub const MyDupeEnum_A: MyDupeEnum = MyDupeEnum(0);
-pub const MyDupeEnum_A_alias: MyDupeEnum = MyDupeEnum(0);
-pub const MyDupeEnum_B: MyDupeEnum = MyDupeEnum(1);
+impl MyDupeEnum {
+ pub const A: MyDupeEnum = MyDupeEnum(0);
+}
+impl MyDupeEnum {
+ pub const A_alias: MyDupeEnum = MyDupeEnum(0);
+}
+impl MyDupeEnum {
+ pub const B: MyDupeEnum = MyDupeEnum(1);
+}
impl ::std::ops::BitOr<MyDupeEnum> for MyDupeEnum {
type Output = Self;
#[inline]
@@ -34,9 +40,15 @@ impl ::std::ops::BitAndAssign for MyDupeEnum {
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct MyDupeEnum(pub u32);
-pub const MyOtherDupeEnum_C: MyOtherDupeEnum = MyOtherDupeEnum(0);
-pub const MyOtherDupeEnum_C_alias: MyOtherDupeEnum = MyOtherDupeEnum(0);
-pub const MyOtherDupeEnum_D: MyOtherDupeEnum = MyOtherDupeEnum(1);
+impl MyOtherDupeEnum {
+ pub const C: MyOtherDupeEnum = MyOtherDupeEnum(0);
+}
+impl MyOtherDupeEnum {
+ pub const C_alias: MyOtherDupeEnum = MyOtherDupeEnum(0);
+}
+impl MyOtherDupeEnum {
+ pub const D: MyOtherDupeEnum = MyOtherDupeEnum(1);
+}
impl ::std::ops::BitOr<MyOtherDupeEnum> for MyOtherDupeEnum {
type Output = Self;
#[inline]