Skip to content

Commit d82292e

Browse files
Ignore extern items in incorrect-case check
1 parent 1341a98 commit d82292e

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

crates/hir_def/src/data.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct FunctionData {
2828
pub has_body: bool,
2929
pub is_unsafe: bool,
3030
pub is_varargs: bool,
31+
pub is_extern: bool,
3132
pub visibility: RawVisibility,
3233
}
3334

@@ -46,6 +47,7 @@ impl FunctionData {
4647
has_body: func.has_body,
4748
is_unsafe: func.is_unsafe,
4849
is_varargs: func.is_varargs,
50+
is_extern: func.is_extern,
4951
visibility: item_tree[func.visibility].clone(),
5052
})
5153
}
@@ -191,6 +193,7 @@ pub struct StaticData {
191193
pub type_ref: TypeRef,
192194
pub visibility: RawVisibility,
193195
pub mutable: bool,
196+
pub is_extern: bool,
194197
}
195198

196199
impl StaticData {
@@ -204,6 +207,7 @@ impl StaticData {
204207
type_ref: statik.type_ref.clone(),
205208
visibility: item_tree[statik.visibility].clone(),
206209
mutable: statik.mutable,
210+
is_extern: statik.is_extern,
207211
})
208212
}
209213
}

crates/hir_def/src/item_tree.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ pub struct Function {
507507
pub has_self_param: bool,
508508
pub has_body: bool,
509509
pub is_unsafe: bool,
510+
/// Whether the function is located in an `extern` block (*not* whether it is an
511+
/// `extern "abi" fn`).
512+
pub is_extern: bool,
510513
pub params: Box<[TypeRef]>,
511514
pub is_varargs: bool,
512515
pub ret_type: TypeRef,
@@ -565,6 +568,8 @@ pub struct Static {
565568
pub name: Name,
566569
pub visibility: RawVisibilityId,
567570
pub mutable: bool,
571+
/// Whether the static is in an `extern` block.
572+
pub is_extern: bool,
568573
pub type_ref: TypeRef,
569574
pub ast_id: FileAstId<ast::Static>,
570575
}

crates/hir_def/src/item_tree/lower.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl Ctx {
340340
has_self_param,
341341
has_body,
342342
is_unsafe: func.unsafe_token().is_some(),
343+
is_extern: false,
343344
params: params.into_boxed_slice(),
344345
is_varargs,
345346
ret_type,
@@ -378,7 +379,7 @@ impl Ctx {
378379
let visibility = self.lower_visibility(static_);
379380
let mutable = static_.mut_token().is_some();
380381
let ast_id = self.source_ast_id_map.ast_id(static_);
381-
let res = Static { name, visibility, mutable, type_ref, ast_id };
382+
let res = Static { name, visibility, mutable, type_ref, ast_id, is_extern: false };
382383
Some(id(self.data().statics.alloc(res)))
383384
}
384385

@@ -554,13 +555,15 @@ impl Ctx {
554555
let attrs = Attrs::new(&item, &self.hygiene);
555556
let id: ModItem = match item {
556557
ast::ExternItem::Fn(ast) => {
557-
let func = self.lower_function(&ast)?;
558-
self.data().functions[func.index].is_unsafe =
559-
is_intrinsic_fn_unsafe(&self.data().functions[func.index].name);
560-
func.into()
558+
let func_id = self.lower_function(&ast)?;
559+
let func = &mut self.data().functions[func_id.index];
560+
func.is_unsafe = is_intrinsic_fn_unsafe(&func.name);
561+
func.is_extern = true;
562+
func_id.into()
561563
}
562564
ast::ExternItem::Static(ast) => {
563565
let statik = self.lower_static(&ast)?;
566+
self.data().statics[statik.index].is_extern = true;
564567
statik.into()
565568
}
566569
ast::ExternItem::TypeAlias(ty) => {

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
8787

8888
fn validate_func(&mut self, db: &dyn HirDatabase, func: FunctionId) {
8989
let data = db.function_data(func);
90+
if data.is_extern {
91+
return;
92+
}
93+
9094
let body = db.body(func.into());
9195

9296
// Recursively validate inner scope items, such as static variables and constants.
@@ -648,6 +652,9 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
648652

649653
fn validate_static(&mut self, db: &dyn HirDatabase, static_id: StaticId) {
650654
let data = db.static_data(static_id);
655+
if data.is_extern {
656+
return;
657+
}
651658

652659
if self.allowed(db, static_id.into(), allow::NON_UPPER_CASE_GLOBAL) {
653660
return;
@@ -920,4 +927,16 @@ fn main() {
920927
"#,
921928
);
922929
}
930+
931+
#[test]
932+
fn ignores_extern_items() {
933+
check_diagnostics(
934+
r#"
935+
extern {
936+
fn NonSnakeCaseName(SOME_VAR: u8) -> u8;
937+
pub static SomeStatic: u8 = 10;
938+
}
939+
"#,
940+
);
941+
}
923942
}

0 commit comments

Comments
 (0)