Ignore extern items in incorrect-case check

This commit is contained in:
Jonas Schievink 2020-12-10 15:45:01 +01:00
parent 1341a98f05
commit d82292e1ce
4 changed files with 36 additions and 5 deletions

View file

@ -28,6 +28,7 @@ pub struct FunctionData {
pub has_body: bool, pub has_body: bool,
pub is_unsafe: bool, pub is_unsafe: bool,
pub is_varargs: bool, pub is_varargs: bool,
pub is_extern: bool,
pub visibility: RawVisibility, pub visibility: RawVisibility,
} }
@ -46,6 +47,7 @@ impl FunctionData {
has_body: func.has_body, has_body: func.has_body,
is_unsafe: func.is_unsafe, is_unsafe: func.is_unsafe,
is_varargs: func.is_varargs, is_varargs: func.is_varargs,
is_extern: func.is_extern,
visibility: item_tree[func.visibility].clone(), visibility: item_tree[func.visibility].clone(),
}) })
} }
@ -191,6 +193,7 @@ pub struct StaticData {
pub type_ref: TypeRef, pub type_ref: TypeRef,
pub visibility: RawVisibility, pub visibility: RawVisibility,
pub mutable: bool, pub mutable: bool,
pub is_extern: bool,
} }
impl StaticData { impl StaticData {
@ -204,6 +207,7 @@ impl StaticData {
type_ref: statik.type_ref.clone(), type_ref: statik.type_ref.clone(),
visibility: item_tree[statik.visibility].clone(), visibility: item_tree[statik.visibility].clone(),
mutable: statik.mutable, mutable: statik.mutable,
is_extern: statik.is_extern,
}) })
} }
} }

View file

@ -507,6 +507,9 @@ pub struct Function {
pub has_self_param: bool, pub has_self_param: bool,
pub has_body: bool, pub has_body: bool,
pub is_unsafe: 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 params: Box<[TypeRef]>,
pub is_varargs: bool, pub is_varargs: bool,
pub ret_type: TypeRef, pub ret_type: TypeRef,
@ -565,6 +568,8 @@ pub struct Static {
pub name: Name, pub name: Name,
pub visibility: RawVisibilityId, pub visibility: RawVisibilityId,
pub mutable: bool, pub mutable: bool,
/// Whether the static is in an `extern` block.
pub is_extern: bool,
pub type_ref: TypeRef, pub type_ref: TypeRef,
pub ast_id: FileAstId<ast::Static>, pub ast_id: FileAstId<ast::Static>,
} }

View file

@ -340,6 +340,7 @@ impl Ctx {
has_self_param, has_self_param,
has_body, has_body,
is_unsafe: func.unsafe_token().is_some(), is_unsafe: func.unsafe_token().is_some(),
is_extern: false,
params: params.into_boxed_slice(), params: params.into_boxed_slice(),
is_varargs, is_varargs,
ret_type, ret_type,
@ -378,7 +379,7 @@ impl Ctx {
let visibility = self.lower_visibility(static_); let visibility = self.lower_visibility(static_);
let mutable = static_.mut_token().is_some(); let mutable = static_.mut_token().is_some();
let ast_id = self.source_ast_id_map.ast_id(static_); 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))) Some(id(self.data().statics.alloc(res)))
} }
@ -554,13 +555,15 @@ impl Ctx {
let attrs = Attrs::new(&item, &self.hygiene); let attrs = Attrs::new(&item, &self.hygiene);
let id: ModItem = match item { let id: ModItem = match item {
ast::ExternItem::Fn(ast) => { ast::ExternItem::Fn(ast) => {
let func = self.lower_function(&ast)?; let func_id = self.lower_function(&ast)?;
self.data().functions[func.index].is_unsafe = let func = &mut self.data().functions[func_id.index];
is_intrinsic_fn_unsafe(&self.data().functions[func.index].name); func.is_unsafe = is_intrinsic_fn_unsafe(&func.name);
func.into() func.is_extern = true;
func_id.into()
} }
ast::ExternItem::Static(ast) => { ast::ExternItem::Static(ast) => {
let statik = self.lower_static(&ast)?; let statik = self.lower_static(&ast)?;
self.data().statics[statik.index].is_extern = true;
statik.into() statik.into()
} }
ast::ExternItem::TypeAlias(ty) => { ast::ExternItem::TypeAlias(ty) => {

View file

@ -87,6 +87,10 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
fn validate_func(&mut self, db: &dyn HirDatabase, func: FunctionId) { fn validate_func(&mut self, db: &dyn HirDatabase, func: FunctionId) {
let data = db.function_data(func); let data = db.function_data(func);
if data.is_extern {
return;
}
let body = db.body(func.into()); let body = db.body(func.into());
// Recursively validate inner scope items, such as static variables and constants. // 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) { fn validate_static(&mut self, db: &dyn HirDatabase, static_id: StaticId) {
let data = db.static_data(static_id); let data = db.static_data(static_id);
if data.is_extern {
return;
}
if self.allowed(db, static_id.into(), allow::NON_UPPER_CASE_GLOBAL) { if self.allowed(db, static_id.into(), allow::NON_UPPER_CASE_GLOBAL) {
return; 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;
}
"#,
);
}
} }