@@ -20,7 +20,8 @@ fn run(
20
20
fold_const: fold_const,
21
21
fold_enum: fold_enum,
22
22
fold_res: fold_res,
23
- fold_iface: fold_iface
23
+ fold_iface: fold_iface,
24
+ fold_impl: fold_impl
24
25
with * fold:: default_seq_fold ( srv)
25
26
} ) ;
26
27
fold. fold_crate ( fold, doc)
@@ -291,30 +292,35 @@ fn fold_iface(
291
292
fold : fold:: fold < astsrv:: srv > ,
292
293
doc : doc:: ifacedoc
293
294
) -> doc:: ifacedoc {
294
-
295
- let srv = fold. ctxt ;
296
-
297
295
{
298
- methods: vec:: map ( doc. methods ) { |methoddoc|
299
- {
300
- args: merge_method_arg_tys (
301
- srv,
302
- doc. id ,
303
- methoddoc. args ,
304
- methoddoc. name ) ,
305
- return : merge_method_ret_ty (
306
- srv,
307
- doc. id ,
308
- methoddoc. return ,
309
- methoddoc. name ) ,
310
- sig: get_method_sig ( srv, doc. id , methoddoc. name )
311
- with methoddoc
312
- }
313
- }
296
+ methods: merge_methods ( fold. ctxt , doc. id , doc. methods )
314
297
with doc
315
298
}
316
299
}
317
300
301
+ fn merge_methods (
302
+ srv : astsrv:: srv ,
303
+ item_id : doc:: ast_id ,
304
+ docs : [ doc:: methoddoc ]
305
+ ) -> [ doc:: methoddoc ] {
306
+ vec:: map ( docs) { |doc|
307
+ {
308
+ args: merge_method_arg_tys (
309
+ srv,
310
+ item_id,
311
+ doc. args ,
312
+ doc. name ) ,
313
+ return : merge_method_ret_ty (
314
+ srv,
315
+ item_id,
316
+ doc. return ,
317
+ doc. name ) ,
318
+ sig: get_method_sig ( srv, item_id, doc. name )
319
+ with doc
320
+ }
321
+ }
322
+ }
323
+
318
324
fn merge_method_ret_ty (
319
325
srv : astsrv:: srv ,
320
326
item_id : doc:: ast_id ,
@@ -351,7 +357,19 @@ fn get_method_ret_ty(
351
357
_ { fail "get_method_ret_ty: undocumented invariant" ; }
352
358
}
353
359
}
354
- _ { fail "get_method_ret_ty: undocumented invariant" ; }
360
+ ast_map:: node_item ( @{
361
+ node: ast:: item_impl ( _, _, _, methods) , _
362
+ } ) {
363
+ alt vec:: find ( methods) { |method|
364
+ method. ident == method_name
365
+ } {
366
+ some ( method) {
367
+ ret_ty_to_str ( method. decl )
368
+ }
369
+ _ { fail "get_method_ret_ty: undocumented invariant" ; }
370
+ }
371
+ }
372
+ _ { fail }
355
373
}
356
374
}
357
375
}
@@ -372,10 +390,22 @@ fn get_method_sig(
372
390
some ( method) {
373
391
some ( pprust:: fun_to_str ( method. decl , method. ident , [ ] ) )
374
392
}
375
- _ { fail "get_method_ret_sig: undocumented invariant" ; }
393
+ _ { fail "get_method_sig: undocumented invariant" ; }
394
+ }
395
+ }
396
+ ast_map:: node_item ( @{
397
+ node: ast:: item_impl ( _, _, _, methods) , _
398
+ } ) {
399
+ alt vec:: find ( methods) { |method|
400
+ method. ident == method_name
401
+ } {
402
+ some ( method) {
403
+ some ( pprust:: fun_to_str ( method. decl , method. ident , [ ] ) )
404
+ }
405
+ _ { fail "get_method_sig: undocumented invariant" ; }
376
406
}
377
407
}
378
- _ { fail "get_method_ret_sig : undocumented invariant" ; }
408
+ _ { fail "get_method_sig : undocumented invariant" ; }
379
409
}
380
410
}
381
411
}
@@ -412,10 +442,22 @@ fn get_method_arg_tys(
412
442
some ( method) {
413
443
decl_arg_tys ( method. decl )
414
444
}
415
- _ { fail "get_method_arg_tys: undocumented invariant" ; }
445
+ _ { fail "get_method_arg_tys: expected method" ; }
446
+ }
447
+ }
448
+ ast_map:: node_item ( @{
449
+ node: ast:: item_impl ( _, _, _, methods) , _
450
+ } ) {
451
+ alt vec:: find ( methods) { |method|
452
+ method. ident == method_name
453
+ } {
454
+ some ( method) {
455
+ decl_arg_tys ( method. decl )
456
+ }
457
+ _ { fail "get_method_arg_tys: expected method" ; }
416
458
}
417
459
}
418
- _ { fail "get_method_arg_tys: undocumented invariant" ; }
460
+ _ { fail }
419
461
}
420
462
}
421
463
}
@@ -457,3 +499,97 @@ fn should_add_iface_method_arg_types() {
457
499
assert fn_. args [ 0 ] . ty == some ( "int" ) ;
458
500
assert fn_. args [ 1 ] . ty == some ( "bool" ) ;
459
501
}
502
+
503
+ fn fold_impl (
504
+ fold : fold:: fold < astsrv:: srv > ,
505
+ doc : doc:: impldoc
506
+ ) -> doc:: impldoc {
507
+
508
+ let srv = fold. ctxt ;
509
+
510
+ let ( iface_ty, self_ty) = astsrv:: exec ( srv) { |ctxt|
511
+ alt ctxt. ast_map . get ( doc. id ) {
512
+ ast_map:: node_item ( @{
513
+ node: ast:: item_impl ( _, iface_ty, self_ty, _) , _
514
+ } ) {
515
+ let iface_ty = option:: map ( iface_ty) { |iface_ty|
516
+ pprust:: ty_to_str ( iface_ty)
517
+ } ;
518
+ ( iface_ty, some ( pprust:: ty_to_str ( self_ty) ) )
519
+ }
520
+ _ { fail "expected impl" }
521
+ }
522
+ } ;
523
+
524
+ {
525
+ iface_ty: iface_ty,
526
+ self_ty: self_ty,
527
+ methods: merge_methods ( fold. ctxt , doc. id , doc. methods )
528
+ with doc
529
+ }
530
+ }
531
+
532
+ #[ test]
533
+ fn should_add_impl_iface_ty ( ) {
534
+ let source = "impl i of j for int { fn a() { } }" ;
535
+ let srv = astsrv:: mk_srv_from_str ( source) ;
536
+ let doc = extract:: from_srv ( srv, "" ) ;
537
+ let doc = run ( srv, doc) ;
538
+ assert doc. topmod . impls ( ) [ 0 ] . iface_ty == some ( "j" ) ;
539
+ }
540
+
541
+ #[ test]
542
+ fn should_not_add_impl_iface_ty_if_none ( ) {
543
+ let source = "impl i for int { fn a() { } }" ;
544
+ let srv = astsrv:: mk_srv_from_str ( source) ;
545
+ let doc = extract:: from_srv ( srv, "" ) ;
546
+ let doc = run ( srv, doc) ;
547
+ assert doc. topmod . impls ( ) [ 0 ] . iface_ty == none;
548
+ }
549
+
550
+ #[ test]
551
+ fn should_add_impl_self_ty ( ) {
552
+ let source = "impl i for int { fn a() { } }" ;
553
+ let srv = astsrv:: mk_srv_from_str ( source) ;
554
+ let doc = extract:: from_srv ( srv, "" ) ;
555
+ let doc = run ( srv, doc) ;
556
+ assert doc. topmod . impls ( ) [ 0 ] . self_ty == some ( "int" ) ;
557
+ }
558
+
559
+ #[ test]
560
+ fn should_add_impl_method_sigs ( ) {
561
+ let source = "impl i for int { fn a() -> int { fail } }" ;
562
+ let srv = astsrv:: mk_srv_from_str ( source) ;
563
+ let doc = extract:: from_srv ( srv, "" ) ;
564
+ let doc = run ( srv, doc) ;
565
+ assert doc. topmod . impls ( ) [ 0 ] . methods [ 0 ] . sig == some ( "fn a() -> int" ) ;
566
+ }
567
+
568
+ #[ test]
569
+ fn should_add_impl_method_ret_types ( ) {
570
+ let source = "impl i for int { fn a() -> int { fail } }" ;
571
+ let srv = astsrv:: mk_srv_from_str ( source) ;
572
+ let doc = extract:: from_srv ( srv, "" ) ;
573
+ let doc = run ( srv, doc) ;
574
+ assert doc. topmod . impls ( ) [ 0 ] . methods [ 0 ] . return . ty == some ( "int" ) ;
575
+ }
576
+
577
+ #[ test]
578
+ fn should_not_add_impl_method_nil_ret_type ( ) {
579
+ let source = "impl i for int { fn a() { } }" ;
580
+ let srv = astsrv:: mk_srv_from_str ( source) ;
581
+ let doc = extract:: from_srv ( srv, "" ) ;
582
+ let doc = run ( srv, doc) ;
583
+ assert doc. topmod . impls ( ) [ 0 ] . methods [ 0 ] . return . ty == none;
584
+ }
585
+
586
+ #[ test]
587
+ fn should_add_impl_method_arg_types ( ) {
588
+ let source = "impl i for int { fn a(b: int, c: bool) { } }" ;
589
+ let srv = astsrv:: mk_srv_from_str ( source) ;
590
+ let doc = extract:: from_srv ( srv, "" ) ;
591
+ let doc = run ( srv, doc) ;
592
+ let fn_ = doc. topmod . impls ( ) [ 0 ] . methods [ 0 ] ;
593
+ assert fn_. args [ 0 ] . ty == some ( "int" ) ;
594
+ assert fn_. args [ 1 ] . ty == some ( "bool" ) ;
595
+ }
0 commit comments