1212//! `unsafe`.
1313use self :: RootUnsafeContext :: * ;
1414
15- use ty:: { self , Ty , TyCtxt } ;
15+ use ty:: { self , TyCtxt } ;
1616use lint;
1717
1818use syntax:: ast;
@@ -40,14 +40,6 @@ enum RootUnsafeContext {
4040 UnsafeBlock ( ast:: NodeId ) ,
4141}
4242
43- fn type_is_unsafe_function ( ty : Ty ) -> bool {
44- match ty. sty {
45- ty:: TyFnDef ( .., f) |
46- ty:: TyFnPtr ( f) => f. unsafety ( ) == hir:: Unsafety :: Unsafe ,
47- _ => false ,
48- }
49- }
50-
5143struct EffectCheckVisitor < ' a , ' tcx : ' a > {
5244 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
5345 tables : & ' a ty:: TypeckTables < ' tcx > ,
@@ -174,10 +166,11 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
174166 match expr. node {
175167 hir:: ExprMethodCall ( ..) => {
176168 let def_id = self . tables . type_dependent_defs [ & expr. id ] . def_id ( ) ;
177- let base_type = self . tcx . type_of ( def_id) ;
178- debug ! ( "effect: method call case, base type is {:?}" ,
179- base_type) ;
180- if type_is_unsafe_function ( base_type) {
169+ let sig = self . tcx . fn_sig ( def_id) ;
170+ debug ! ( "effect: method call case, signature is {:?}" ,
171+ sig) ;
172+
173+ if sig. 0 . unsafety == hir:: Unsafety :: Unsafe {
181174 self . require_unsafe ( expr. span ,
182175 "invocation of unsafe method" )
183176 }
@@ -186,8 +179,13 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
186179 let base_type = self . tables . expr_ty_adjusted ( base) ;
187180 debug ! ( "effect: call case, base type is {:?}" ,
188181 base_type) ;
189- if type_is_unsafe_function ( base_type) {
190- self . require_unsafe ( expr. span , "call to unsafe function" )
182+ match base_type. sty {
183+ ty:: TyFnDef ( ..) | ty:: TyFnPtr ( _) => {
184+ if base_type. fn_sig ( self . tcx ) . unsafety ( ) == hir:: Unsafety :: Unsafe {
185+ self . require_unsafe ( expr. span , "call to unsafe function" )
186+ }
187+ }
188+ _ => { }
191189 }
192190 }
193191 hir:: ExprUnary ( hir:: UnDeref , ref base) => {
0 commit comments