Skip to content

Commit

Permalink
Add diagnostic for accessing an extern static
Browse files Browse the repository at this point in the history
  • Loading branch information
ChayimFriedman2 committed Aug 29, 2024
1 parent 28142e4 commit 8bb235b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ fn walk_unsafe(
let g = resolver.update_to_inner_scope(db.upcast(), def, current);
let value_or_partial = resolver.resolve_path_in_value_ns(db.upcast(), path);
if let Some(ResolveValueResult::ValueNs(ValueNs::StaticId(id), _)) = value_or_partial {
if db.static_data(id).mutable {
let static_data = db.static_data(id);
if static_data.mutable || static_data.is_extern {
unsafe_expr_cb(UnsafeExpr { expr: current, inside_unsafe_block });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ fn main() {
);
}

#[test]
fn missing_unsafe_diagnostic_with_extern_static() {
check_diagnostics(
r#"
//- minicore: copy
extern "C" {
static EXTERN: i32;
static mut EXTERN_MUT: i32;
}
fn main() {
let _x = EXTERN;
//^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
let _x = EXTERN_MUT;
//^^^^^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
unsafe {
let _x = EXTERN;
let _x = EXTERN_MUT;
}
}
"#,
);
}

#[test]
fn no_missing_unsafe_diagnostic_with_safe_intrinsic() {
check_diagnostics(
Expand Down

0 comments on commit 8bb235b

Please sign in to comment.