7
7
import metadata:: csearch:: { each_path, get_impl_traits, get_impls_for_mod} ;
8
8
import metadata:: cstore:: { cstore, iter_crate_data} ;
9
9
import metadata:: decoder:: { dl_def, dl_field, dl_impl} ;
10
- import middle:: resolve3:: { Impl , MethodInfo } ;
10
+ import middle:: resolve3:: Impl ;
11
11
import middle:: ty:: { get, lookup_item_type, subst, t, ty_box} ;
12
12
import middle:: ty:: { ty_uniq, ty_ptr, ty_rptr, ty_enum} ;
13
13
import middle:: ty:: { ty_class, ty_nil, ty_bot, ty_bool, ty_int, ty_uint} ;
@@ -108,16 +108,6 @@ fn get_base_type_def_id(inference_context: infer_ctxt,
108
108
}
109
109
}
110
110
111
-
112
- fn method_to_MethodInfo ( ast_method : @method ) -> @MethodInfo {
113
- @{
114
- did: local_def ( ast_method. id ) ,
115
- n_tps: ast_method. tps . len ( ) ,
116
- ident: ast_method. ident ,
117
- self_type: ast_method. self_ty . node
118
- }
119
- }
120
-
121
111
class CoherenceInfo {
122
112
// Contains implementations of methods that are inherent to a type.
123
113
// Methods in these implementations don't need to be exported.
@@ -161,70 +151,10 @@ class CoherenceChecker {
161
151
self . privileged_types = new_def_hash ( ) ;
162
152
}
163
153
164
- // Create a mapping containing a MethodInfo for every provided
165
- // method in every trait.
166
- fn build_provided_methods_map ( crate : @crate ) {
167
-
168
- let pmm = self . crate_context . provided_methods_map ;
169
-
170
- visit_crate ( * crate , ( ) , mk_simple_visitor ( @{
171
- visit_item: |item| {
172
- match item. node {
173
- item_trait( _, _, trait_methods) => {
174
- for trait_methods. each |trait_method| {
175
- debug ! { "(building provided methods map) checking \
176
- trait `%s` with id %d", * item. ident, item. id} ;
177
-
178
- match trait_method {
179
- required( _) => { /* fall through */ }
180
- provided( m) => {
181
- // For every provided method in the
182
- // trait, store a MethodInfo.
183
- let mi = method_to_MethodInfo ( m) ;
184
-
185
- match pmm. find ( item. id ) {
186
- some( mis) => {
187
- // If the trait already has an
188
- // entry in the
189
- // provided_methods_map, we just
190
- // need to add this method to
191
- // that entry.
192
- debug ! { "(building provided \
193
- methods map) adding \
194
- method `%s` to entry for \
195
- existing trait",
196
- * mi. ident} ;
197
- let mut method_infos = mis;
198
- push ( method_infos, mi) ;
199
- pmm. insert ( item. id , method_infos) ;
200
- }
201
- none => {
202
- // If the trait doesn't have an
203
- // entry yet, create one.
204
- debug ! { "(building provided \
205
- methods map) creating new \
206
- entry for method `%s`",
207
- * mi. ident} ;
208
- pmm. insert ( item. id , ~[ mi] ) ;
209
- }
210
- }
211
- }
212
- }
213
- }
214
- }
215
- _ => {
216
- // Nothing to do.
217
- }
218
- } ;
219
- }
220
- with * default_simple_visitor ( )
221
- } ) ) ;
222
- }
223
-
224
154
fn check_coherence ( crate : @crate ) {
225
-
226
155
// Check implementations. This populates the tables containing the
227
156
// inherent methods and extension methods.
157
+
228
158
visit_crate ( * crate , ( ) , mk_simple_visitor ( @{
229
159
visit_item: |item| {
230
160
debug ! { "(checking coherence) item '%s'" , * item. ident} ;
@@ -257,6 +187,7 @@ class CoherenceChecker {
257
187
// Bring in external crates. It's fine for this to happen after the
258
188
// coherence checks, because we ensure by construction that no errors
259
189
// can happen at link time.
190
+
260
191
self . add_external_crates ( ) ;
261
192
}
262
193
@@ -272,8 +203,8 @@ class CoherenceChecker {
272
203
* item. ident} ;
273
204
274
205
match get_base_type_def_id ( self . inference_context ,
275
- item. span ,
276
- self_type. ty ) {
206
+ item. span ,
207
+ self_type. ty ) {
277
208
none => {
278
209
let session = self . crate_context . tcx . sess ;
279
210
session. span_err ( item. span ,
@@ -420,6 +351,7 @@ class CoherenceChecker {
420
351
}
421
352
422
353
// Privileged scope checking
354
+
423
355
fn check_privileged_scopes( crate : @crate ) {
424
356
// Gather up all privileged types.
425
357
let privileged_types =
@@ -498,13 +430,15 @@ class CoherenceChecker {
498
430
// trait was defined in this
499
431
// crate.
500
432
501
- let trait_def_id =
502
- self . trait_ref_to_trait_def_id (
503
- trait_ref) ;
504
-
505
- if trait_def_id. crate != local_crate {
506
- let session =
507
- self . crate_context . tcx . sess ;
433
+ let def_map = self . crate_context . tcx
434
+ . def_map ;
435
+ let trait_def = def_map. get
436
+ ( trait_ref. ref_id ) ;
437
+ let trait_id =
438
+ def_id_of_def ( trait_def) ;
439
+ if trait_id. crate != local_crate {
440
+ let session = self . crate_context
441
+ . tcx . sess ;
508
442
session. span_err ( item. span ,
509
443
~"cannot \
510
444
provide an \
@@ -532,13 +466,6 @@ class CoherenceChecker {
532
466
}));
533
467
}
534
468
535
- fn trait_ref_to_trait_def_id(trait_ref: @trait_ref) -> def_id {
536
- let def_map = self.crate_context.tcx.def_map;
537
- let trait_def = def_map.get(trait_ref.ref_id);
538
- let trait_id = def_id_of_def(trait_def);
539
- return trait_id;
540
- }
541
-
542
469
fn gather_privileged_types(items: ~[@item]) -> @dvec<def_id> {
543
470
let results = @dvec();
544
471
for items.each |item| {
@@ -560,70 +487,16 @@ class CoherenceChecker {
560
487
561
488
// Converts an implementation in the AST to an Impl structure.
562
489
fn create_impl_from_item(item: @item) -> @Impl {
563
-
564
- fn add_provided_methods(inherent_methods: ~[@MethodInfo],
565
- all_provided_methods: ~[@MethodInfo])
566
- -> ~[@MethodInfo] {
567
-
568
- let mut methods = inherent_methods;
569
-
570
- // If there's no inherent method with the same name as a
571
- // provided method, add that provided method to `methods`.
572
- for all_provided_methods.each |provided_method| {
573
- let mut method_inherent_to_impl = false;
574
- for inherent_methods.each |inherent_method| {
575
- if provided_method.ident == inherent_method.ident {
576
- method_inherent_to_impl = true;
577
- }
578
- }
579
-
580
- if !method_inherent_to_impl {
581
- debug!{" ( creating impl) adding provided method `%s` to \
582
- impl ", * provided_method. ident } ;
583
- push( methods, provided_method) ;
584
- }
585
- }
586
-
587
- return methods;
588
- }
589
-
590
490
match item.node {
591
- item_impl ( ty_params, trait_refs , _, ast_methods) => {
491
+ item_impl(ty_params, _ , _, ast_methods) => {
592
492
let mut methods = ~[];
593
-
594
493
for ast_methods.each |ast_method| {
595
- push ( methods,
596
- method_to_MethodInfo ( ast_method) ) ;
597
- }
598
-
599
- // For each trait that the impl implements, see what
600
- // methods are provided. For each of those methods,
601
- // if a method of that name is not inherent to the
602
- // impl, use the provided definition in the trait.
603
- for trait_refs. each |trait_ref| {
604
-
605
- let trait_did = self . trait_ref_to_trait_def_id ( trait_ref) ;
606
-
607
- match self . crate_context . provided_methods_map
608
- . find ( trait_did. node ) {
609
- none => {
610
- debug ! { "(creating impl) trait with node_id `%d` \
611
- has no provided methods", trait_did. node} ;
612
- /* fall through */
613
- }
614
- some( all_provided)
615
- => {
616
- debug ! { "(creating impl) trait with node_id `%d` \
617
- has provided methods", trait_did. node} ;
618
- // Selectively add only those provided
619
- // methods that aren't inherent to the
620
- // trait.
621
-
622
- // XXX: could probably be doing this with filter.
623
- methods = add_provided_methods ( methods,
624
- all_provided) ;
625
- }
626
- }
494
+ push(methods, @{
495
+ did: local_def(ast_method.id),
496
+ n_tps: ast_method.tps.len(),
497
+ ident: ast_method.ident,
498
+ self_type: ast_method.self_ty.node
499
+ });
627
500
}
628
501
629
502
return @{
@@ -715,8 +588,8 @@ class CoherenceChecker {
715
588
716
589
if associated_traits. len ( ) == 0 {
717
590
match get_base_type_def_id ( self . inference_context ,
718
- dummy_sp ( ) ,
719
- self_type. ty ) {
591
+ dummy_sp ( ) ,
592
+ self_type. ty ) {
720
593
none => {
721
594
let session = self . crate_context . tcx . sess ;
722
595
session. bug ( fmt ! { "no base type for external impl \
@@ -796,8 +669,6 @@ class CoherenceChecker {
796
669
}
797
670
798
671
fn check_coherence( crate_context: @crate_ctxt, crate : @crate ) {
799
- let coherence_checker = @CoherenceChecker ( crate_context) ;
800
- ( * coherence_checker) . build_provided_methods_map( crate ) ;
801
- ( * coherence_checker) . check_coherence( crate ) ;
672
+ CoherenceChecker ( crate_context) . check_coherence( crate ) ;
802
673
}
803
674
0 commit comments