@@ -6,6 +6,7 @@ For soundness's sake, be as defensive as possible:
6
6
- explicitly match all fields of the Rust AST so we catch any features added in the future
7
7
*/
8
8
9
+ use crate :: context:: Context ;
9
10
use crate :: rust_to_vir_adts:: { check_item_enum, check_item_struct} ;
10
11
use crate :: rust_to_vir_base:: { hack_check_def_name, hack_get_def_name} ;
11
12
use crate :: rust_to_vir_func:: { check_foreign_item_fn, check_item_fn} ;
@@ -22,20 +23,18 @@ use std::rc::Rc;
22
23
use vir:: ast:: { Krate , KrateX , VirErr } ;
23
24
24
25
fn check_item < ' tcx > (
25
- tcx : TyCtxt < ' tcx > ,
26
- krate : & ' tcx Crate < ' tcx > ,
26
+ ctxt : & ' tcx Context < ' tcx > ,
27
27
vir : & mut KrateX ,
28
28
id : & ItemId ,
29
29
item : & ' tcx Item < ' tcx > ,
30
30
) -> Result < ( ) , VirErr > {
31
31
match & item. kind {
32
32
ItemKind :: Fn ( sig, generics, body_id) => {
33
33
check_item_fn (
34
- tcx,
35
- krate,
34
+ ctxt,
36
35
vir,
37
36
item. ident ,
38
- tcx. hir ( ) . attrs ( item. hir_id ( ) ) ,
37
+ ctxt . tcx . hir ( ) . attrs ( item. hir_id ( ) ) ,
39
38
sig,
40
39
generics,
41
40
body_id,
@@ -49,30 +48,31 @@ fn check_item<'tcx>(
49
48
// TODO use rustc_middle info here? if sufficient, it may allow for a single code path
50
49
// for definitions of the local crate and imported crates
51
50
// let adt_def = tcx.adt_def(item.def_id);
52
- check_item_struct ( tcx , krate , vir, item. span , id, variant_data, generics) ?;
51
+ check_item_struct ( ctxt , vir, item. span , id, variant_data, generics) ?;
53
52
}
54
53
ItemKind :: Enum ( enum_def, generics) => {
55
54
// TODO use rustc_middle? see `Struct` case
56
- check_item_enum ( tcx , krate , vir, item. span , id, enum_def, generics) ?;
55
+ check_item_enum ( ctxt , vir, item. span , id, enum_def, generics) ?;
57
56
}
58
57
ItemKind :: Impl ( impll) => {
59
58
if let Some ( TraitRef { path, hir_ref_id : _ } ) = impll. of_trait {
60
59
unsupported_err_unless ! (
61
- hack_check_def_name( tcx, path. res. def_id( ) , "core" , "marker::StructuralEq" )
62
- || hack_check_def_name( tcx, path. res. def_id( ) , "core" , "cmp::Eq" )
60
+ ctxt,
61
+ hack_check_def_name( ctxt. tcx, path. res. def_id( ) , "core" , "marker::StructuralEq" )
62
+ || hack_check_def_name( ctxt. tcx, path. res. def_id( ) , "core" , "cmp::Eq" )
63
63
|| hack_check_def_name(
64
- tcx,
64
+ ctxt . tcx,
65
65
path. res. def_id( ) ,
66
66
"core" ,
67
67
"marker::StructuralPartialEq"
68
68
)
69
- || hack_check_def_name( tcx, path. res. def_id( ) , "core" , "cmp::PartialEq" )
70
- || hack_check_def_name( tcx, path. res. def_id( ) , "builtin" , "Structural" ) ,
69
+ || hack_check_def_name( ctxt . tcx, path. res. def_id( ) , "core" , "cmp::PartialEq" )
70
+ || hack_check_def_name( ctxt . tcx, path. res. def_id( ) , "builtin" , "Structural" ) ,
71
71
item. span,
72
72
"non_eq_trait_impl" ,
73
73
path
74
74
) ;
75
- if hack_check_def_name ( tcx, path. res . def_id ( ) , "builtin" , "Structural" ) {
75
+ if hack_check_def_name ( ctxt . tcx , path. res . def_id ( ) , "builtin" , "Structural" ) {
76
76
let ty = {
77
77
// TODO extract to rust_to_vir_base, or use
78
78
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/fn.hir_ty_to_ty.html
@@ -86,40 +86,43 @@ fn check_item<'tcx>(
86
86
impll. self_ty. kind
87
87
) ,
88
88
} ;
89
- tcx. type_of ( def_id)
89
+ ctxt . tcx . type_of ( def_id)
90
90
} ;
91
91
// TODO: this may be a bit of a hack: to query the TyCtxt for the StructuralEq impl it seems we need
92
92
// a concrete type, so apply ! to all type parameters
93
93
let ty_kind_applied_never =
94
94
if let rustc_middle:: ty:: TyKind :: Adt ( def, substs) = ty. kind ( ) {
95
95
rustc_middle:: ty:: TyKind :: Adt (
96
96
def,
97
- tcx. mk_substs ( substs. iter ( ) . map ( |g| match g. unpack ( ) {
97
+ ctxt . tcx . mk_substs ( substs. iter ( ) . map ( |g| match g. unpack ( ) {
98
98
rustc_middle:: ty:: subst:: GenericArgKind :: Type ( _) => {
99
- ( * tcx) . types . never . into ( )
99
+ ( * ctxt . tcx ) . types . never . into ( )
100
100
}
101
101
_ => g,
102
102
} ) ) ,
103
103
)
104
104
} else {
105
105
panic ! ( "Structural impl for non-adt type" ) ;
106
106
} ;
107
- let ty_applied_never = tcx. mk_ty ( ty_kind_applied_never) ;
107
+ let ty_applied_never = ctxt . tcx . mk_ty ( ty_kind_applied_never) ;
108
108
err_unless ! (
109
- ty_applied_never. is_structural_eq_shallow( tcx) ,
109
+ ctxt,
110
+ ty_applied_never. is_structural_eq_shallow( ctxt. tcx) ,
110
111
item. span,
111
112
format!( "Structural impl for non-structural type {:?}" , ty) ,
112
113
ty
113
114
) ;
114
115
}
115
116
} else {
116
117
unsupported_err_unless ! (
118
+ ctxt,
117
119
impll. of_trait. is_none( ) ,
118
120
item. span,
119
121
"unsupported impl of trait" ,
120
122
item
121
123
) ;
122
124
unsupported_err_unless ! (
125
+ ctxt,
123
126
impll. generics. params. len( ) == 0 ,
124
127
item. span,
125
128
"unsupported impl of non-trait with generics" ,
@@ -129,26 +132,27 @@ fn check_item<'tcx>(
129
132
TyKind :: Path ( QPath :: Resolved ( _, _path) ) => {
130
133
for impl_item in impll. items {
131
134
// TODO once we have references
132
- unsupported_err ! ( item. span, "unsupported method in impl" , impl_item) ;
135
+ unsupported_err ! ( ctxt , item. span, "unsupported method in impl" , impl_item) ;
133
136
}
134
137
}
135
138
_ => {
136
- unsupported_err ! ( item. span, "unsupported impl of non-path type" , item) ;
139
+ unsupported_err ! ( ctxt , item. span, "unsupported impl of non-path type" , item) ;
137
140
}
138
141
}
139
142
}
140
143
}
141
144
ItemKind :: Const ( _ty, _body_id) => {
142
145
unsupported_err_unless ! (
143
- hack_get_def_name( tcx, _body_id. hir_id. owner. to_def_id( ) )
146
+ ctxt,
147
+ hack_get_def_name( ctxt. tcx, _body_id. hir_id. owner. to_def_id( ) )
144
148
. starts_with( "_DERIVE_builtin_Structural_FOR_" ) ,
145
149
item. span,
146
150
"unsupported const" ,
147
151
item
148
152
) ;
149
153
}
150
154
_ => {
151
- unsupported_err ! ( item. span, "unsupported item" , item) ;
155
+ unsupported_err ! ( ctxt , item. span, "unsupported item" , item) ;
152
156
}
153
157
}
154
158
Ok ( ( ) )
@@ -187,26 +191,26 @@ fn check_module<'tcx>(
187
191
}
188
192
189
193
fn check_foreign_item < ' tcx > (
190
- tcx : TyCtxt < ' tcx > ,
194
+ ctxt : & Context < ' tcx > ,
191
195
vir : & mut KrateX ,
192
196
_id : & ForeignItemId ,
193
197
item : & ' tcx ForeignItem < ' tcx > ,
194
198
) -> Result < ( ) , VirErr > {
195
199
match & item. kind {
196
200
ForeignItemKind :: Fn ( decl, idents, generics) => {
197
201
check_foreign_item_fn (
198
- tcx ,
202
+ ctxt ,
199
203
vir,
200
204
item. ident ,
201
205
item. span ,
202
- tcx. hir ( ) . attrs ( item. hir_id ( ) ) ,
206
+ ctxt . tcx . hir ( ) . attrs ( item. hir_id ( ) ) ,
203
207
decl,
204
208
idents,
205
209
generics,
206
210
) ?;
207
211
}
208
212
_ => {
209
- unsupported_err ! ( item. span, "unsupported item" , item) ;
213
+ unsupported_err ! ( ctxt , item. span, "unsupported item" , item) ;
210
214
}
211
215
}
212
216
Ok ( ( ) )
@@ -221,7 +225,7 @@ fn check_attr<'tcx>(
221
225
Ok ( ( ) )
222
226
}
223
227
224
- pub fn crate_to_vir < ' tcx > ( tcx : TyCtxt < ' tcx > , krate : & ' tcx Crate < ' tcx > ) -> Result < Krate , VirErr > {
228
+ pub fn crate_to_vir < ' tcx > ( ctxt : & Context < ' tcx > ) -> Result < Krate , VirErr > {
225
229
let Crate {
226
230
item : _,
227
231
exported_macros,
@@ -237,7 +241,7 @@ pub fn crate_to_vir<'tcx>(tcx: TyCtxt<'tcx>, krate: &'tcx Crate<'tcx>) -> Result
237
241
proc_macros,
238
242
trait_map,
239
243
attrs,
240
- } = krate;
244
+ } = ctxt . krate ;
241
245
let mut vir: KrateX = Default :: default ( ) ;
242
246
unsupported_unless ! (
243
247
exported_macros. len( ) == 0 ,
@@ -250,10 +254,10 @@ pub fn crate_to_vir<'tcx>(tcx: TyCtxt<'tcx>, krate: &'tcx Crate<'tcx>) -> Result
250
254
non_exported_macro_attrs
251
255
) ;
252
256
for ( id, item) in foreign_items {
253
- check_foreign_item ( tcx , & mut vir, id, item) ?;
257
+ check_foreign_item ( ctxt , & mut vir, id, item) ?;
254
258
}
255
259
for ( id, item) in items {
256
- check_item ( tcx , krate , & mut vir, id, item) ?;
260
+ check_item ( ctxt , & mut vir, id, item) ?;
257
261
}
258
262
unsupported_unless ! ( trait_items. len( ) == 0 , "trait definitions" , trait_items) ;
259
263
for ( _id, impl_item) in impl_items {
@@ -270,22 +274,22 @@ pub fn crate_to_vir<'tcx>(tcx: TyCtxt<'tcx>, krate: &'tcx Crate<'tcx>) -> Result
270
274
}
271
275
for ( id, _trait_impl) in trait_impls {
272
276
unsupported_unless ! (
273
- hack_check_def_name( tcx, * id, "core" , "marker::StructuralEq" )
274
- || hack_check_def_name( tcx, * id, "core" , "cmp::Eq" )
275
- || hack_check_def_name( tcx, * id, "core" , "marker::StructuralPartialEq" )
276
- || hack_check_def_name( tcx, * id, "core" , "cmp::PartialEq" )
277
- || hack_check_def_name( tcx, * id, "builtin" , "Structural" ) ,
277
+ hack_check_def_name( ctxt . tcx, * id, "core" , "marker::StructuralEq" )
278
+ || hack_check_def_name( ctxt . tcx, * id, "core" , "cmp::Eq" )
279
+ || hack_check_def_name( ctxt . tcx, * id, "core" , "marker::StructuralPartialEq" )
280
+ || hack_check_def_name( ctxt . tcx, * id, "core" , "cmp::PartialEq" )
281
+ || hack_check_def_name( ctxt . tcx, * id, "builtin" , "Structural" ) ,
278
282
"non_eq_trait_impl" ,
279
283
id
280
284
) ;
281
285
}
282
286
for ( id, module) in modules {
283
- check_module ( tcx, id, module) ?;
287
+ check_module ( ctxt . tcx , id, module) ?;
284
288
}
285
289
unsupported_unless ! ( proc_macros. len( ) == 0 , "procedural macros" , proc_macros) ;
286
290
unsupported_unless ! ( trait_map. iter( ) . all( |( _, v) | v. len( ) == 0 ) , "traits" , trait_map) ;
287
291
for ( id, attr) in attrs {
288
- check_attr ( tcx, id, attr) ?;
292
+ check_attr ( ctxt . tcx , id, attr) ?;
289
293
}
290
294
Ok ( Rc :: new ( vir) )
291
295
}
0 commit comments