Respect visibility of constants (#314)

Fixes #123:
 - Adds visibility check for module-level constants.
 - Adds visibility check for associated constants.
 - Fixes bitflags expansion to produce public associated constants (as the real expansion does).
This commit is contained in:
Ingvar Stepanyan
2019-04-16 10:25:23 +01:00
committed by Emilio Cobos Álvarez
parent 8d0d69d347
commit 8ea3b2d25f
14 changed files with 45 additions and 27 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ impl Flag {
} = *self; } = *self;
quote! { quote! {
#(#attrs)* #(#attrs)*
const #name : #struct_name = #struct_name { bits: #value }; pub const #name : #struct_name = #struct_name { bits: #value };
} }
} }
} }
+12
View File
@@ -691,6 +691,12 @@ impl Parse {
let impl_path = ty.unwrap().get_root_path().unwrap(); let impl_path = ty.unwrap().get_root_path().unwrap();
for item in items.into_iter() { for item in items.into_iter() {
if let syn::Visibility::Public(_) = item.vis {
} else {
warn!("Skip {}::{} - (not `pub`).", crate_name, &item.ident);
return;
}
let path = Path::new(item.ident.to_string()); let path = Path::new(item.ident.to_string());
match Constant::load( match Constant::load(
path, path,
@@ -739,6 +745,12 @@ impl Parse {
return; return;
} }
if let syn::Visibility::Public(_) = item.vis {
} else {
warn!("Skip {}::{} - (not `pub`).", crate_name, &item.ident);
return;
}
let path = Path::new(item.ident.to_string()); let path = Path::new(item.ident.to_string());
match Constant::load(path, mod_cfg, &item.ty, &item.expr, &item.attrs, None) { match Constant::load(path, mod_cfg, &item.ty, &item.expr, &item.attrs, None) {
Ok(constant) => { Ok(constant) => {
+1 -1
View File
@@ -1,7 +1,7 @@
#[repr(C)] #[repr(C)]
struct Foo {} struct Foo {}
const Foo_FOO: u32 = 42; pub const Foo_FOO: u32 = 42;
impl Foo { impl Foo {
const FOO: i32 = 0; const FOO: i32 = 0;
+6 -3
View File
@@ -2,9 +2,12 @@
struct Foo {} struct Foo {}
impl Foo { impl Foo {
const GA: i32 = 10; pub const GA: i32 = 10;
const BU: &'static str = "hello world"; pub const BU: &'static str = "hello world";
const ZO: f32 = 3.14; pub const ZO: f32 = 3.14;
pub(crate) const DONT_EXPORT_CRATE: i32 = 20;
const DONT_EXPORT_PRIV: i32 = 30;
} }
#[no_mangle] #[no_mangle]
+1 -1
View File
@@ -5,5 +5,5 @@ impl Foo {
const FOO: i32 = 0; const FOO: i32 = 0;
} }
const Foo_FOO: u32 = 42; pub const Foo_FOO: u32 = 42;
+1 -1
View File
@@ -1,4 +1,4 @@
#[repr(transparent)] #[repr(transparent)]
struct Transparent { field: u8 } struct Transparent { field: u8 }
const FOO: Transparent = Transparent { field: 0 }; pub const FOO: Transparent = Transparent { field: 0 };
+6 -3
View File
@@ -1,12 +1,15 @@
const FOO: i32 = 10; pub const FOO: i32 = 10;
const BAR: &'static str = "hello world"; pub const BAR: &'static str = "hello world";
pub const DELIMITER: char = ':'; pub const DELIMITER: char = ':';
pub const LEFTCURLY: char = '{'; pub const LEFTCURLY: char = '{';
pub const QUOTE: char = '\''; pub const QUOTE: char = '\'';
pub const TAB: char = '\t'; pub const TAB: char = '\t';
pub const NEWLINE: char = '\n'; pub const NEWLINE: char = '\n';
pub const HEART: char = '❤'; pub const HEART: char = '❤';
const ZOM: f32 = 3.14; pub const ZOM: f32 = 3.14;
pub(crate) const DONT_EXPORT_CRATE: i32 = 20;
const DONT_EXPORT_PRIV: i32 = 30;
#[repr(C)] #[repr(C)]
struct Foo { struct Foo {
+3 -3
View File
@@ -1,6 +1,6 @@
const FOO: i32 = 10; pub const FOO: i32 = 10;
const BAR: &'static str = "hello world"; pub const BAR: &'static str = "hello world";
const ZOM: f32 = 3.14; pub const ZOM: f32 = 3.14;
#[repr(C)] #[repr(C)]
struct Foo { struct Foo {
+3 -3
View File
@@ -1,6 +1,6 @@
const FOO: i32 = 10; pub const FOO: i32 = 10;
const BAR: &'static str = "hello world"; pub const BAR: &'static str = "hello world";
const ZOM: f32 = 3.14; pub const ZOM: f32 = 3.14;
#[repr(C)] #[repr(C)]
struct Foo { struct Foo {
+1 -1
View File
@@ -1,4 +1,4 @@
const LEN: i32 = 42; pub const LEN: i32 = 42;
pub type NamedLenArray = [i32; LEN]; pub type NamedLenArray = [i32; LEN];
pub type ValuedLenArray = [i32; 42]; pub type ValuedLenArray = [i32; 42];
+2 -2
View File
@@ -5,10 +5,10 @@ struct Foo {
} }
impl Foo { impl Foo {
const FOO: Foo = Foo{ a: 42, b: 47, }; pub const FOO: Foo = Foo{ a: 42, b: 47, };
} }
const BAR: Foo = Foo{ a: 42, b: 1337, }; pub const BAR: Foo = Foo{ a: 42, b: 1337, };
#[no_mangle] #[no_mangle]
pub extern "C" fn root(x: Foo) { } pub extern "C" fn root(x: Foo) { }
+1 -1
View File
@@ -10,7 +10,7 @@ struct Bar {
a: i32, a: i32,
} }
const VAL: Foo = Foo { pub const VAL: Foo = Foo {
a: 42, a: 42,
b: 1337, b: 1337,
bar: Bar { a: 323 }, bar: Bar { a: 323 },
+1 -1
View File
@@ -31,7 +31,7 @@ type F = A;
#[no_mangle] #[no_mangle]
pub static G: i32 = 10; pub static G: i32 = 10;
const H: i32 = 10; pub const H: i32 = 10;
#[no_mangle] #[no_mangle]
pub extern "C" fn root( pub extern "C" fn root(
+6 -6
View File
@@ -10,14 +10,14 @@ struct Bar {
} }
impl Foo { impl Foo {
const FOO: Foo = Foo { a: 42, b: 47, }; pub const FOO: Foo = Foo { a: 42, b: 47, };
const FOO2: Self = Foo { a: 42, b: 47, }; pub const FOO2: Self = Foo { a: 42, b: 47, };
const FOO3: Self = Self { a: 42, b: 47, }; pub const FOO3: Self = Self { a: 42, b: 47, };
const BAZ: Bar = Bar { a: 42, b: 47, }; pub const BAZ: Bar = Bar { a: 42, b: 47, };
} }
const BAR: Foo = Foo { a: 42, b: 1337, }; pub const BAR: Foo = Foo { a: 42, b: 1337, };
const BAZZ: Bar = Bar { a: 42, b: 1337, }; pub const BAZZ: Bar = Bar { a: 42, b: 1337, };
#[no_mangle] #[no_mangle]
pub extern "C" fn root(x: Foo, bar: Bar) { } pub extern "C" fn root(x: Foo, bar: Bar) { }