33use std:: fmt:: { self , Write } ;
44
55use itertools:: Itertools ;
6+ use span:: Edition ;
67
78use crate :: {
89 hir:: {
@@ -15,20 +16,26 @@ use crate::{
1516
1617use super :: * ;
1718
18- pub ( super ) fn print_body_hir ( db : & dyn DefDatabase , body : & Body , owner : DefWithBodyId ) -> String {
19+ pub ( super ) fn print_body_hir (
20+ db : & dyn DefDatabase ,
21+ body : & Body ,
22+ owner : DefWithBodyId ,
23+ edition : Edition ,
24+ ) -> String {
1925 let header = match owner {
20- DefWithBodyId :: FunctionId ( it) => {
21- it. lookup ( db) . id . resolved ( db, |it| format ! ( "fn {}" , it. name. display( db. upcast( ) ) ) )
22- }
26+ DefWithBodyId :: FunctionId ( it) => it
27+ . lookup ( db)
28+ . id
29+ . resolved ( db, |it| format ! ( "fn {}" , it. name. display( db. upcast( ) , edition) ) ) ,
2330 DefWithBodyId :: StaticId ( it) => it
2431 . lookup ( db)
2532 . id
26- . resolved ( db, |it| format ! ( "static {} = " , it. name. display( db. upcast( ) ) ) ) ,
33+ . resolved ( db, |it| format ! ( "static {} = " , it. name. display( db. upcast( ) , edition ) ) ) ,
2734 DefWithBodyId :: ConstId ( it) => it. lookup ( db) . id . resolved ( db, |it| {
2835 format ! (
2936 "const {} = " ,
3037 match & it. name {
31- Some ( name) => name. display( db. upcast( ) ) . to_string( ) ,
38+ Some ( name) => name. display( db. upcast( ) , edition ) . to_string( ) ,
3239 None => "_" . to_owned( ) ,
3340 }
3441 )
@@ -39,13 +46,13 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
3946 let enum_loc = loc. parent . lookup ( db) ;
4047 format ! (
4148 "enum {}::{}" ,
42- enum_loc. id. item_tree( db) [ enum_loc. id. value] . name. display( db. upcast( ) ) ,
43- loc. id. item_tree( db) [ loc. id. value] . name. display( db. upcast( ) ) ,
49+ enum_loc. id. item_tree( db) [ enum_loc. id. value] . name. display( db. upcast( ) , edition ) ,
50+ loc. id. item_tree( db) [ loc. id. value] . name. display( db. upcast( ) , edition ) ,
4451 )
4552 }
4653 } ;
4754
48- let mut p = Printer { db, body, buf : header, indent_level : 0 , needs_indent : false } ;
55+ let mut p = Printer { db, body, buf : header, indent_level : 0 , needs_indent : false , edition } ;
4956 if let DefWithBodyId :: FunctionId ( it) = owner {
5057 p. buf . push ( '(' ) ;
5158 let function_data = & db. function_data ( it) ;
@@ -86,8 +93,10 @@ pub(super) fn print_expr_hir(
8693 body : & Body ,
8794 _owner : DefWithBodyId ,
8895 expr : ExprId ,
96+ edition : Edition ,
8997) -> String {
90- let mut p = Printer { db, body, buf : String :: new ( ) , indent_level : 0 , needs_indent : false } ;
98+ let mut p =
99+ Printer { db, body, buf : String :: new ( ) , indent_level : 0 , needs_indent : false , edition } ;
91100 p. print_expr ( expr) ;
92101 p. buf
93102}
@@ -113,6 +122,7 @@ struct Printer<'a> {
113122 buf : String ,
114123 indent_level : usize ,
115124 needs_indent : bool ,
125+ edition : Edition ,
116126}
117127
118128impl Write for Printer < ' _ > {
@@ -173,13 +183,14 @@ impl Printer<'_> {
173183 Expr :: OffsetOf ( offset_of) => {
174184 w ! ( self , "builtin#offset_of(" ) ;
175185 self . print_type_ref ( & offset_of. container ) ;
186+ let edition = self . edition ;
176187 w ! (
177188 self ,
178189 ", {})" ,
179190 offset_of
180191 . fields
181192 . iter( )
182- . format_with( "." , |field, f| f( & field. display( self . db. upcast( ) ) ) )
193+ . format_with( "." , |field, f| f( & field. display( self . db. upcast( ) , edition ) ) )
183194 ) ;
184195 }
185196 Expr :: Path ( path) => self . print_path ( path) ,
@@ -201,7 +212,7 @@ impl Printer<'_> {
201212 }
202213 Expr :: Loop { body, label } => {
203214 if let Some ( lbl) = label {
204- w ! ( self , "{}: " , self . body[ * lbl] . name. display( self . db. upcast( ) ) ) ;
215+ w ! ( self , "{}: " , self . body[ * lbl] . name. display( self . db. upcast( ) , self . edition ) ) ;
205216 }
206217 w ! ( self , "loop " ) ;
207218 self . print_expr ( * body) ;
@@ -221,10 +232,11 @@ impl Printer<'_> {
221232 }
222233 Expr :: MethodCall { receiver, method_name, args, generic_args } => {
223234 self . print_expr ( * receiver) ;
224- w ! ( self , ".{}" , method_name. display( self . db. upcast( ) ) ) ;
235+ w ! ( self , ".{}" , method_name. display( self . db. upcast( ) , self . edition ) ) ;
225236 if let Some ( args) = generic_args {
226237 w ! ( self , "::<" ) ;
227- print_generic_args ( self . db , args, self ) . unwrap ( ) ;
238+ let edition = self . edition ;
239+ print_generic_args ( self . db , args, self , edition) . unwrap ( ) ;
228240 w ! ( self , ">" ) ;
229241 }
230242 w ! ( self , "(" ) ;
@@ -259,13 +271,13 @@ impl Printer<'_> {
259271 Expr :: Continue { label } => {
260272 w ! ( self , "continue" ) ;
261273 if let Some ( lbl) = label {
262- w ! ( self , " {}" , self . body[ * lbl] . name. display( self . db. upcast( ) ) ) ;
274+ w ! ( self , " {}" , self . body[ * lbl] . name. display( self . db. upcast( ) , self . edition ) ) ;
263275 }
264276 }
265277 Expr :: Break { expr, label } => {
266278 w ! ( self , "break" ) ;
267279 if let Some ( lbl) = label {
268- w ! ( self , " {}" , self . body[ * lbl] . name. display( self . db. upcast( ) ) ) ;
280+ w ! ( self , " {}" , self . body[ * lbl] . name. display( self . db. upcast( ) , self . edition ) ) ;
269281 }
270282 if let Some ( expr) = expr {
271283 self . whitespace ( ) ;
@@ -307,9 +319,10 @@ impl Printer<'_> {
307319 }
308320
309321 w ! ( self , "{{" ) ;
322+ let edition = self . edition ;
310323 self . indented ( |p| {
311324 for field in & * * fields {
312- w ! ( p, "{}: " , field. name. display( self . db. upcast( ) ) ) ;
325+ w ! ( p, "{}: " , field. name. display( self . db. upcast( ) , edition ) ) ;
313326 p. print_expr ( field. expr ) ;
314327 wln ! ( p, "," ) ;
315328 }
@@ -326,7 +339,7 @@ impl Printer<'_> {
326339 }
327340 Expr :: Field { expr, name } => {
328341 self . print_expr ( * expr) ;
329- w ! ( self , ".{}" , name. display( self . db. upcast( ) ) ) ;
342+ w ! ( self , ".{}" , name. display( self . db. upcast( ) , self . edition ) ) ;
330343 }
331344 Expr :: Await { expr } => {
332345 self . print_expr ( * expr) ;
@@ -464,8 +477,9 @@ impl Printer<'_> {
464477 }
465478 Expr :: Literal ( lit) => self . print_literal ( lit) ,
466479 Expr :: Block { id : _, statements, tail, label } => {
467- let label =
468- label. map ( |lbl| format ! ( "{}: " , self . body[ lbl] . name. display( self . db. upcast( ) ) ) ) ;
480+ let label = label. map ( |lbl| {
481+ format ! ( "{}: " , self . body[ lbl] . name. display( self . db. upcast( ) , self . edition) )
482+ } ) ;
469483 self . print_block ( label. as_deref ( ) , statements, tail) ;
470484 }
471485 Expr :: Unsafe { id : _, statements, tail } => {
@@ -539,9 +553,10 @@ impl Printer<'_> {
539553 }
540554
541555 w ! ( self , " {{" ) ;
556+ let edition = self . edition ;
542557 self . indented ( |p| {
543558 for arg in args. iter ( ) {
544- w ! ( p, "{}: " , arg. name. display( self . db. upcast( ) ) ) ;
559+ w ! ( p, "{}: " , arg. name. display( self . db. upcast( ) , edition ) ) ;
545560 p. print_pat ( arg. pat ) ;
546561 wln ! ( p, "," ) ;
547562 }
@@ -686,11 +701,13 @@ impl Printer<'_> {
686701 }
687702
688703 fn print_type_ref ( & mut self , ty : & TypeRef ) {
689- print_type_ref ( self . db , ty, self ) . unwrap ( ) ;
704+ let edition = self . edition ;
705+ print_type_ref ( self . db , ty, self , edition) . unwrap ( ) ;
690706 }
691707
692708 fn print_path ( & mut self , path : & Path ) {
693- print_path ( self . db , path, self ) . unwrap ( ) ;
709+ let edition = self . edition ;
710+ print_path ( self . db , path, self , edition) . unwrap ( ) ;
694711 }
695712
696713 fn print_binding ( & mut self , id : BindingId ) {
@@ -701,6 +718,6 @@ impl Printer<'_> {
701718 BindingAnnotation :: Ref => "ref " ,
702719 BindingAnnotation :: RefMut => "ref mut " ,
703720 } ;
704- w ! ( self , "{}{}" , mode, name. display( self . db. upcast( ) ) ) ;
721+ w ! ( self , "{}{}" , mode, name. display( self . db. upcast( ) , self . edition ) ) ;
705722 }
706723}
0 commit comments