From d82292e1ce8112cfa5e42d0221a563649d067747 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 10 Dec 2020 15:45:01 +0100 Subject: [PATCH] Ignore extern items in incorrect-case check --- crates/hir_def/src/data.rs | 4 ++++ crates/hir_def/src/item_tree.rs | 5 +++++ crates/hir_def/src/item_tree/lower.rs | 13 ++++++++----- crates/hir_ty/src/diagnostics/decl_check.rs | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index 14604593832..dd3a906af0c 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs @@ -28,6 +28,7 @@ pub struct FunctionData { pub has_body: bool, pub is_unsafe: bool, pub is_varargs: bool, + pub is_extern: bool, pub visibility: RawVisibility, } @@ -46,6 +47,7 @@ impl FunctionData { has_body: func.has_body, is_unsafe: func.is_unsafe, is_varargs: func.is_varargs, + is_extern: func.is_extern, visibility: item_tree[func.visibility].clone(), }) } @@ -191,6 +193,7 @@ pub struct StaticData { pub type_ref: TypeRef, pub visibility: RawVisibility, pub mutable: bool, + pub is_extern: bool, } impl StaticData { @@ -204,6 +207,7 @@ impl StaticData { type_ref: statik.type_ref.clone(), visibility: item_tree[statik.visibility].clone(), mutable: statik.mutable, + is_extern: statik.is_extern, }) } } diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 7eb388baed5..b3ec252fd59 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -507,6 +507,9 @@ pub struct Function { pub has_self_param: bool, pub has_body: bool, pub is_unsafe: bool, + /// Whether the function is located in an `extern` block (*not* whether it is an + /// `extern "abi" fn`). + pub is_extern: bool, pub params: Box<[TypeRef]>, pub is_varargs: bool, pub ret_type: TypeRef, @@ -565,6 +568,8 @@ pub struct Static { pub name: Name, pub visibility: RawVisibilityId, pub mutable: bool, + /// Whether the static is in an `extern` block. + pub is_extern: bool, pub type_ref: TypeRef, pub ast_id: FileAstId, } diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index ca7fb4a43db..63b2826f857 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -340,6 +340,7 @@ impl Ctx { has_self_param, has_body, is_unsafe: func.unsafe_token().is_some(), + is_extern: false, params: params.into_boxed_slice(), is_varargs, ret_type, @@ -378,7 +379,7 @@ impl Ctx { let visibility = self.lower_visibility(static_); let mutable = static_.mut_token().is_some(); let ast_id = self.source_ast_id_map.ast_id(static_); - let res = Static { name, visibility, mutable, type_ref, ast_id }; + let res = Static { name, visibility, mutable, type_ref, ast_id, is_extern: false }; Some(id(self.data().statics.alloc(res))) } @@ -554,13 +555,15 @@ impl Ctx { let attrs = Attrs::new(&item, &self.hygiene); let id: ModItem = match item { ast::ExternItem::Fn(ast) => { - let func = self.lower_function(&ast)?; - self.data().functions[func.index].is_unsafe = - is_intrinsic_fn_unsafe(&self.data().functions[func.index].name); - func.into() + let func_id = self.lower_function(&ast)?; + let func = &mut self.data().functions[func_id.index]; + func.is_unsafe = is_intrinsic_fn_unsafe(&func.name); + func.is_extern = true; + func_id.into() } ast::ExternItem::Static(ast) => { let statik = self.lower_static(&ast)?; + self.data().statics[statik.index].is_extern = true; statik.into() } ast::ExternItem::TypeAlias(ty) => { diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index 4b3e2fa8f66..724bad867dd 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs @@ -87,6 +87,10 @@ impl<'a, 'b> DeclValidator<'a, 'b> { fn validate_func(&mut self, db: &dyn HirDatabase, func: FunctionId) { let data = db.function_data(func); + if data.is_extern { + return; + } + let body = db.body(func.into()); // Recursively validate inner scope items, such as static variables and constants. @@ -648,6 +652,9 @@ impl<'a, 'b> DeclValidator<'a, 'b> { fn validate_static(&mut self, db: &dyn HirDatabase, static_id: StaticId) { let data = db.static_data(static_id); + if data.is_extern { + return; + } if self.allowed(db, static_id.into(), allow::NON_UPPER_CASE_GLOBAL) { return; @@ -920,4 +927,16 @@ fn main() { "#, ); } + + #[test] + fn ignores_extern_items() { + check_diagnostics( + r#" +extern { + fn NonSnakeCaseName(SOME_VAR: u8) -> u8; + pub static SomeStatic: u8 = 10; +} + "#, + ); + } }