@@ -17,7 +17,7 @@ use hir_def::{
1717 ConstParamId , FunctionId , GenericDefId , ItemContainerId , Lookup , TraitId , TypeAliasId ,
1818 TypeOrConstParamId , TypeParamId ,
1919} ;
20- use hir_expand:: name:: { known , Name } ;
20+ use hir_expand:: name:: Name ;
2121use itertools:: Either ;
2222use rustc_hash:: FxHashSet ;
2323use smallvec:: { smallvec, SmallVec } ;
@@ -335,54 +335,18 @@ pub fn is_fn_unsafe_to_call(db: &dyn HirDatabase, func: FunctionId) -> bool {
335335 // Function in an `extern` block are always unsafe to call, except when it has
336336 // `"rust-intrinsic"` ABI there are a few exceptions.
337337 let id = block. lookup ( db. upcast ( ) ) . id ;
338- !matches ! (
339- id. item_tree( db. upcast( ) ) [ id. value] . abi. as_deref( ) ,
340- Some ( "rust-intrinsic" ) if !is_intrinsic_fn_unsafe( & data. name)
341- )
338+
339+ let is_intrinsic =
340+ id. item_tree ( db. upcast ( ) ) [ id. value ] . abi . as_deref ( ) == Some ( "rust-intrinsic" ) ;
341+
342+ if is_intrinsic {
343+ // Intrinsics are unsafe unless they have the rustc_safe_intrinsic attribute
344+ !data. attrs . by_key ( "rustc_safe_intrinsic" ) . exists ( )
345+ } else {
346+ // Extern items are always unsafe
347+ true
348+ }
342349 }
343350 _ => false ,
344351 }
345352}
346-
347- /// Returns `true` if the given intrinsic is unsafe to call, or false otherwise.
348- fn is_intrinsic_fn_unsafe ( name : & Name ) -> bool {
349- // Should be kept in sync with https://github.com/rust-lang/rust/blob/532d2b14c05f9bc20b2d27cbb5f4550d28343a36/compiler/rustc_typeck/src/check/intrinsic.rs#L72-L106
350- ![
351- known:: abort,
352- known:: add_with_overflow,
353- known:: bitreverse,
354- known:: black_box,
355- known:: bswap,
356- known:: caller_location,
357- known:: ctlz,
358- known:: ctpop,
359- known:: cttz,
360- known:: discriminant_value,
361- known:: forget,
362- known:: likely,
363- known:: maxnumf32,
364- known:: maxnumf64,
365- known:: min_align_of,
366- known:: minnumf32,
367- known:: minnumf64,
368- known:: mul_with_overflow,
369- known:: needs_drop,
370- known:: ptr_guaranteed_eq,
371- known:: ptr_guaranteed_ne,
372- known:: rotate_left,
373- known:: rotate_right,
374- known:: rustc_peek,
375- known:: saturating_add,
376- known:: saturating_sub,
377- known:: size_of,
378- known:: sub_with_overflow,
379- known:: type_id,
380- known:: type_name,
381- known:: unlikely,
382- known:: variant_count,
383- known:: wrapping_add,
384- known:: wrapping_mul,
385- known:: wrapping_sub,
386- ]
387- . contains ( name)
388- }
0 commit comments