@@ -349,7 +349,7 @@ impl Compiler {
349349 let size_before = self . code_stack . len ( ) ;
350350 self . symbol_table_stack . push ( symbol_table) ;
351351
352- let ( doc, statements) = split_doc ( body) ;
352+ let ( doc, statements) = split_doc ( body, & self . opts ) ;
353353 if let Some ( value) = doc {
354354 self . emit_constant ( ConstantData :: Str { value } ) ;
355355 let doc = self . name ( "__doc__" ) ;
@@ -1187,7 +1187,7 @@ impl Compiler {
11871187 let qualified_name = self . qualified_path . join ( "." ) ;
11881188 self . push_qualified_path ( "<locals>" ) ;
11891189
1190- let ( doc_str, body) = split_doc ( body) ;
1190+ let ( doc_str, body) = split_doc ( body, & self . opts ) ;
11911191
11921192 self . current_code_info ( )
11931193 . constants
@@ -1383,7 +1383,7 @@ impl Compiler {
13831383
13841384 self . push_output ( bytecode:: CodeFlags :: empty ( ) , 0 , 0 , 0 , name. to_owned ( ) ) ;
13851385
1386- let ( doc_str, body) = split_doc ( body) ;
1386+ let ( doc_str, body) = split_doc ( body, & self . opts ) ;
13871387
13881388 let dunder_name = self . name ( "__name__" ) ;
13891389 emit ! ( self , Instruction :: LoadGlobal ( dunder_name) ) ;
@@ -2875,10 +2875,17 @@ impl EmitArg<bytecode::Label> for ir::BlockIdx {
28752875 }
28762876}
28772877
2878- fn split_doc ( body : & [ located_ast:: Stmt ] ) -> ( Option < String > , & [ located_ast:: Stmt ] ) {
2878+ fn split_doc < ' a > (
2879+ body : & ' a [ located_ast:: Stmt ] ,
2880+ opts : & CompileOpts ,
2881+ ) -> ( Option < String > , & ' a [ located_ast:: Stmt ] ) {
28792882 if let Some ( ( located_ast:: Stmt :: Expr ( expr) , body_rest) ) = body. split_first ( ) {
28802883 if let Some ( doc) = try_get_constant_string ( std:: slice:: from_ref ( & expr. value ) ) {
2881- return ( Some ( doc) , body_rest) ;
2884+ if opts. optimize < 2 {
2885+ return ( Some ( doc) , body_rest) ;
2886+ } else {
2887+ return ( None , body_rest) ;
2888+ }
28822889 }
28832890 }
28842891 ( None , body)
0 commit comments