@@ -15,8 +15,9 @@ use hir_def::{
1515use span:: Edition ;
1616
1717use crate :: {
18- InferenceResult , Interner , TargetFeatures , TyExt , TyKind , db:: HirDatabase ,
19- utils:: is_fn_unsafe_to_call,
18+ InferenceResult , Interner , TargetFeatures , TyExt , TyKind ,
19+ db:: HirDatabase ,
20+ utils:: { is_fn_unsafe_to_call, target_feature_is_safe_in_target} ,
2021} ;
2122
2223#[ derive( Debug , Default ) ]
@@ -144,6 +145,9 @@ struct UnsafeVisitor<'db> {
144145 def_target_features : TargetFeatures ,
145146 // FIXME: This needs to be the edition of the span of each call.
146147 edition : Edition ,
148+ /// On some targets (WASM), calling safe functions with `#[target_feature]` is always safe, even when
149+ /// the target feature is not enabled. This flag encodes that.
150+ target_feature_is_safe : bool ,
147151}
148152
149153impl < ' db > UnsafeVisitor < ' db > {
@@ -159,7 +163,12 @@ impl<'db> UnsafeVisitor<'db> {
159163 DefWithBodyId :: FunctionId ( func) => TargetFeatures :: from_attrs ( & db. attrs ( func. into ( ) ) ) ,
160164 _ => TargetFeatures :: default ( ) ,
161165 } ;
162- let edition = resolver. module ( ) . krate ( ) . data ( db) . edition ;
166+ let krate = resolver. module ( ) . krate ( ) ;
167+ let edition = krate. data ( db) . edition ;
168+ let target_feature_is_safe = match & krate. workspace_data ( db) . target {
169+ Ok ( target) => target_feature_is_safe_in_target ( target) ,
170+ Err ( _) => false ,
171+ } ;
163172 Self {
164173 db,
165174 infer,
@@ -172,6 +181,7 @@ impl<'db> UnsafeVisitor<'db> {
172181 callback : unsafe_expr_cb,
173182 def_target_features,
174183 edition,
184+ target_feature_is_safe,
175185 }
176186 }
177187
@@ -184,7 +194,13 @@ impl<'db> UnsafeVisitor<'db> {
184194 }
185195
186196 fn check_call ( & mut self , node : ExprId , func : FunctionId ) {
187- let unsafety = is_fn_unsafe_to_call ( self . db , func, & self . def_target_features , self . edition ) ;
197+ let unsafety = is_fn_unsafe_to_call (
198+ self . db ,
199+ func,
200+ & self . def_target_features ,
201+ self . edition ,
202+ self . target_feature_is_safe ,
203+ ) ;
188204 match unsafety {
189205 crate :: utils:: Unsafety :: Safe => { }
190206 crate :: utils:: Unsafety :: Unsafe => {
0 commit comments