@@ -598,17 +598,7 @@ impl Compiler {
598
598
self . in_function_def = true ;
599
599
let mut flags = self . enter_function ( name, args) ?;
600
600
601
- let doc = get_doc ( body) ;
602
- let ( new_body, doc_str) = match doc {
603
- Some ( val) => {
604
- if let Some ( ( _, body_rest) ) = body. split_first ( ) {
605
- ( body_rest, val)
606
- } else {
607
- ( body, "" . to_string ( ) )
608
- }
609
- }
610
- None => ( body, "" . to_string ( ) ) ,
611
- } ;
601
+ let ( new_body, doc_str) = get_doc ( body) ;
612
602
613
603
self . compile_statements ( new_body) ?;
614
604
@@ -657,10 +647,6 @@ impl Compiler {
657
647
} ) ;
658
648
}
659
649
660
- self . emit ( Instruction :: LoadConst {
661
- value : bytecode:: Constant :: String { value : doc_str } ,
662
- } ) ;
663
-
664
650
self . emit ( Instruction :: LoadConst {
665
651
value : bytecode:: Constant :: Code {
666
652
code : Box :: new ( code) ,
@@ -679,6 +665,20 @@ impl Compiler {
679
665
self . emit ( Instruction :: StoreName {
680
666
name : name. to_string ( ) ,
681
667
} ) ;
668
+
669
+ if let Some ( doc_string) = doc_str {
670
+ self . emit ( Instruction :: LoadConst {
671
+ value : bytecode:: Constant :: String {
672
+ value : doc_string. to_string ( ) ,
673
+ } ,
674
+ } ) ;
675
+ self . emit ( Instruction :: LoadName {
676
+ name : name. to_string ( ) ,
677
+ } ) ;
678
+ self . emit ( Instruction :: StoreAttr {
679
+ name : "__doc__" . to_string ( ) ,
680
+ } ) ;
681
+ }
682
682
self . in_loop = was_in_loop;
683
683
self . in_function_def = was_in_function_def;
684
684
Ok ( ( ) )
@@ -707,17 +707,7 @@ impl Compiler {
707
707
name. to_string ( ) ,
708
708
) ) ;
709
709
710
- let doc = get_doc ( body) ;
711
- let ( new_body, doc_str) = match doc {
712
- Some ( val) => {
713
- if let Some ( ( _, body_rest) ) = body. split_first ( ) {
714
- ( body_rest, val)
715
- } else {
716
- ( body, "" . to_string ( ) )
717
- }
718
- }
719
- None => ( body, "" . to_string ( ) ) ,
720
- } ;
710
+ let ( new_body, doc_str) = get_doc ( body) ;
721
711
722
712
self . compile_statements ( new_body) ?;
723
713
self . emit ( Instruction :: LoadConst {
@@ -727,13 +717,6 @@ impl Compiler {
727
717
728
718
let code = self . pop_code_object ( ) ;
729
719
730
- // function doc
731
- self . emit ( Instruction :: LoadConst {
732
- value : bytecode:: Constant :: String {
733
- value : "" . to_string ( ) ,
734
- } ,
735
- } ) ;
736
-
737
720
self . emit ( Instruction :: LoadConst {
738
721
value : bytecode:: Constant :: Code {
739
722
code : Box :: new ( code) ,
@@ -756,11 +739,6 @@ impl Compiler {
756
739
} ,
757
740
} ) ;
758
741
759
- // class doc
760
- self . emit ( Instruction :: LoadConst {
761
- value : bytecode:: Constant :: String { value : doc_str } ,
762
- } ) ;
763
-
764
742
for base in bases {
765
743
self . compile_expression ( base) ?;
766
744
}
@@ -785,11 +763,11 @@ impl Compiler {
785
763
} ,
786
764
} ) ;
787
765
self . emit ( Instruction :: CallFunction {
788
- typ : CallType :: Keyword ( 3 + keywords. len ( ) + bases. len ( ) ) ,
766
+ typ : CallType :: Keyword ( 2 + keywords. len ( ) + bases. len ( ) ) ,
789
767
} ) ;
790
768
} else {
791
769
self . emit ( Instruction :: CallFunction {
792
- typ : CallType :: Positional ( 3 + bases. len ( ) ) ,
770
+ typ : CallType :: Positional ( 2 + bases. len ( ) ) ,
793
771
} ) ;
794
772
}
795
773
@@ -798,6 +776,19 @@ impl Compiler {
798
776
self . emit ( Instruction :: StoreName {
799
777
name : name. to_string ( ) ,
800
778
} ) ;
779
+ if let Some ( doc_string) = doc_str {
780
+ self . emit ( Instruction :: LoadConst {
781
+ value : bytecode:: Constant :: String {
782
+ value : doc_string. to_string ( ) ,
783
+ } ,
784
+ } ) ;
785
+ self . emit ( Instruction :: LoadName {
786
+ name : name. to_string ( ) ,
787
+ } ) ;
788
+ self . emit ( Instruction :: StoreAttr {
789
+ name : "__doc__" . to_string ( ) ,
790
+ } ) ;
791
+ }
801
792
self . in_loop = was_in_loop;
802
793
Ok ( ( ) )
803
794
}
@@ -1168,14 +1159,6 @@ impl Compiler {
1168
1159
self . compile_expression ( body) ?;
1169
1160
self . emit ( Instruction :: ReturnValue ) ;
1170
1161
let code = self . pop_code_object ( ) ;
1171
-
1172
- // function doc
1173
- self . emit ( Instruction :: LoadConst {
1174
- value : bytecode:: Constant :: String {
1175
- value : "" . to_string ( ) ,
1176
- } ,
1177
- } ) ;
1178
-
1179
1162
self . emit ( Instruction :: LoadConst {
1180
1163
value : bytecode:: Constant :: Code {
1181
1164
code : Box :: new ( code) ,
@@ -1462,13 +1445,6 @@ impl Compiler {
1462
1445
// Fetch code for listcomp function:
1463
1446
let code = self . pop_code_object ( ) ;
1464
1447
1465
- // function doc
1466
- self . emit ( Instruction :: LoadConst {
1467
- value : bytecode:: Constant :: String {
1468
- value : "" . to_string ( ) ,
1469
- } ,
1470
- } ) ;
1471
-
1472
1448
// List comprehension code:
1473
1449
self . emit ( Instruction :: LoadConst {
1474
1450
value : bytecode:: Constant :: Code {
@@ -1569,17 +1545,19 @@ impl Compiler {
1569
1545
}
1570
1546
}
1571
1547
1572
- fn get_doc ( body : & [ ast:: LocatedStatement ] ) -> Option < String > {
1548
+ fn get_doc ( body : & [ ast:: LocatedStatement ] ) -> ( & [ ast :: LocatedStatement ] , Option < String > ) {
1573
1549
if let Some ( val) = body. get ( 0 ) {
1574
1550
if let ast:: Statement :: Expression { ref expression } = val. node {
1575
1551
if let ast:: Expression :: String { ref value } = expression {
1576
1552
if let ast:: StringGroup :: Constant { ref value } = value {
1577
- return Some ( value. to_string ( ) ) ;
1553
+ if let Some ( ( _, body_rest) ) = body. split_first ( ) {
1554
+ return ( body_rest, Some ( value. to_string ( ) ) ) ;
1555
+ }
1578
1556
}
1579
1557
}
1580
1558
}
1581
1559
}
1582
- None
1560
+ ( body , None )
1583
1561
}
1584
1562
1585
1563
#[ cfg( test) ]
0 commit comments