-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Added private type leak validation to structs
Also exposed struct field visibility in the HIR and created a struct validation module
- Loading branch information
Showing
7 changed files
with
181 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use super::Struct; | ||
use crate::diagnostics::ExportedPrivate; | ||
use crate::resolve::HasResolver; | ||
use crate::DiagnosticSink; | ||
use crate::{HasVisibility, Ty, Visibility}; | ||
|
||
use crate::FileId; | ||
use crate::HirDatabase; | ||
|
||
use crate::visibility::RawVisibility; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub struct StructValidator<'a> { | ||
strukt: Struct, | ||
db: &'a dyn HirDatabase, | ||
file_id: FileId, | ||
} | ||
|
||
impl<'a> StructValidator<'a> { | ||
pub fn new(strukt: Struct, db: &'a dyn HirDatabase, file_id: FileId) -> Self { | ||
StructValidator { | ||
strukt, | ||
db, | ||
file_id, | ||
} | ||
} | ||
|
||
pub fn validate_privacy(&self, sink: &mut DiagnosticSink) { | ||
let resolver = self.strukt.id.resolver(self.db.upcast()); | ||
let struct_data = self.strukt.data(self.db.upcast()); | ||
|
||
let public_fields = struct_data | ||
.fields | ||
.iter() | ||
.filter(|(_, field_data)| field_data.visibility == RawVisibility::Public); | ||
|
||
let field_types = public_fields.map(|(_, field_data)| { | ||
let type_ref = field_data.type_ref; | ||
let ty = Ty::from_hir(self.db, &resolver, &struct_data.type_ref_map(), type_ref).ty; | ||
(ty, type_ref) | ||
}); | ||
|
||
let struct_visibility = self.strukt.visibility(self.db); | ||
let type_is_allowed = |ty: &Ty| match struct_visibility { | ||
Visibility::Module(module_id) => { | ||
ty.visibility(self.db).is_visible_from(self.db, module_id) | ||
} | ||
Visibility::Public => ty.visibility(self.db).is_externally_visible(), | ||
}; | ||
|
||
field_types | ||
.filter(|(ty, _)| !type_is_allowed(&ty)) | ||
.for_each(|(_, type_ref)| { | ||
sink.push(ExportedPrivate { | ||
file: self.file_id, | ||
type_ref: struct_data | ||
.type_ref_source_map() | ||
.type_ref_syntax(type_ref) | ||
.unwrap(), | ||
}) | ||
}); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...napshots/mun_hir__code_model__r#struct__validator__tests__private_leak_struct_fields.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
source: crates/mun_hir/src/code_model/struct/validator/tests.rs | ||
expression: "struct Foo(usize);\npub struct Bar(usize);\n\n// valid, bar is public\npub struct Baz {\n foo: Foo,\n pub bar: Bar,\n}\n\n// invalid, Foo is private\npub struct FooBar {\n pub foo: Foo,\n pub bar: Bar,\n}\n\n// valid, FooBaz is private\nstruct FooBaz {\n pub foo: Foo,\n pub bar: Bar,\n}\n\npub(crate) struct BarBaz;\n\n// invalid, exporting pub(crate) to pub\npub struct FooBarBaz {\n pub foo: Foo,\n pub bar: Bar,\n}" | ||
|
||
--- | ||
179..182: can't leak private type | ||
391..394: can't leak private type | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#[cfg(test)] | ||
use crate::utils::tests::*; | ||
|
||
#[test] | ||
fn test_private_leak_struct_fields() { | ||
diagnostics_snapshot( | ||
r#" | ||
struct Foo(usize); | ||
pub struct Bar(usize); | ||
// valid, bar is public | ||
pub struct Baz { | ||
foo: Foo, | ||
pub bar: Bar, | ||
} | ||
// invalid, Foo is private | ||
pub struct FooBar { | ||
pub foo: Foo, | ||
pub bar: Bar, | ||
} | ||
// valid, FooBaz is private | ||
struct FooBaz { | ||
pub foo: Foo, | ||
pub bar: Bar, | ||
} | ||
pub(crate) struct BarBaz; | ||
// invalid, exporting pub(crate) to pub | ||
pub struct FooBarBaz { | ||
pub foo: Foo, | ||
pub bar: Bar, | ||
} | ||
"#, | ||
) | ||
} | ||
|
||
// this function needs to be declared in each file separately | ||
// since insta's AutoName creates files in the directory from which | ||
// the macro is called. | ||
fn diagnostics_snapshot(text: &str) { | ||
let text = text.trim().replace("\n ", "\n"); | ||
insta::assert_snapshot!(insta::_macro_support::AutoName, diagnostics(&text), &text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters