@@ -343,17 +343,98 @@ pub(crate) fn is_literal_expr(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
343
343
false
344
344
}
345
345
346
+ /// Build a textual representation of an unevaluated constant expression.
347
+ ///
348
+ /// If the const expression is too complex, an underscore `_` is returned.
349
+ /// For const arguments, it's `{ _ }` to be precise.
350
+ /// This means that the output is not necessarily valid Rust code.
351
+ ///
352
+ /// Currently, only
353
+ ///
354
+ /// * literals (optionally with a leading `-`)
355
+ /// * unit `()`
356
+ /// * blocks (`{ … }`) around simple expressions and
357
+ /// * paths without arguments
358
+ ///
359
+ /// are considered simple enough. Simple blocks are included since they are
360
+ /// necessary to disambiguate unit from the unit type.
361
+ /// This list might get extended in the future.
362
+ ///
363
+ /// Without this censoring, in a lot of cases the output would get too large
364
+ /// and verbose. Consider `match` expressions, blocks and deeply nested ADTs.
365
+ /// Further, private and `doc(hidden)` fields of structs would get leaked
366
+ /// since HIR datatypes like the `body` parameter do not contain enough
367
+ /// semantic information for this function to be able to hide them –
368
+ /// at least not without significant performance overhead.
369
+ ///
370
+ /// Whenever possible, prefer to evaluate the constant first and try to
371
+ /// use a different method for pretty-printing. Ideally this function
372
+ /// should only ever be used as a fallback.
346
373
pub ( crate ) fn print_const_expr ( tcx : TyCtxt < ' _ > , body : hir:: BodyId ) -> String {
347
374
let hir = tcx. hir ( ) ;
348
375
let value = & hir. body ( body) . value ;
349
376
350
- let snippet = if !value. span . from_expansion ( ) {
351
- tcx. sess . source_map ( ) . span_to_snippet ( value. span ) . ok ( )
352
- } else {
353
- None
354
- } ;
377
+ #[ derive( PartialEq , Eq ) ]
378
+ enum Classification {
379
+ Literal ,
380
+ Simple ,
381
+ Complex ,
382
+ }
355
383
356
- snippet. unwrap_or_else ( || rustc_hir_pretty:: id_to_string ( & hir, body. hir_id ) )
384
+ use Classification :: * ;
385
+
386
+ fn classify ( expr : & hir:: Expr < ' _ > ) -> Classification {
387
+ match & expr. kind {
388
+ hir:: ExprKind :: Unary ( hir:: UnOp :: Neg , expr) => {
389
+ if matches ! ( expr. kind, hir:: ExprKind :: Lit ( _) ) { Literal } else { Complex }
390
+ }
391
+ hir:: ExprKind :: Lit ( _) => Literal ,
392
+ hir:: ExprKind :: Tup ( [ ] ) => Simple ,
393
+ hir:: ExprKind :: Block ( hir:: Block { stmts : [ ] , expr : Some ( expr) , .. } , _) => {
394
+ if classify ( expr) == Complex { Complex } else { Simple }
395
+ }
396
+ // Paths with a self-type or arguments are too “complex” following our measure since
397
+ // they may leak private fields of structs (with feature `adt_const_params`).
398
+ // Consider: `<Self as Trait<{ Struct { private: () } }>>::CONSTANT`.
399
+ // Paths without arguments are definitely harmless though.
400
+ hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, hir:: Path { segments, .. } ) ) => {
401
+ if segments. iter ( ) . all ( |segment| segment. args . is_none ( ) ) { Simple } else { Complex }
402
+ }
403
+ // FIXME: Claiming that those kinds of QPaths are simple is probably not true if the Ty
404
+ // contains const arguments. Is there a *concise* way to check for this?
405
+ hir:: ExprKind :: Path ( hir:: QPath :: TypeRelative ( ..) ) => Simple ,
406
+ // FIXME: Can they contain const arguments and thus leak private struct fields?
407
+ hir:: ExprKind :: Path ( hir:: QPath :: LangItem ( ..) ) => Simple ,
408
+ _ => Complex ,
409
+ }
410
+ }
411
+
412
+ let classification = classify ( value) ;
413
+
414
+ if classification == Literal
415
+ && !value. span . from_expansion ( )
416
+ && let Ok ( snippet) = tcx. sess . source_map ( ) . span_to_snippet ( value. span ) {
417
+ // For literals, we avoid invoking the pretty-printer and use the source snippet instead to
418
+ // preserve certain stylistic choices the user likely made for the sake legibility like
419
+ //
420
+ // * hexadecimal notation
421
+ // * underscores
422
+ // * character escapes
423
+ //
424
+ // FIXME: This passes through `-/*spacer*/0` verbatim.
425
+ snippet
426
+ } else if classification == Simple {
427
+ // Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and
428
+ // other formatting artifacts.
429
+ rustc_hir_pretty:: id_to_string ( & hir, body. hir_id )
430
+ } else if tcx. def_kind ( hir. body_owner_def_id ( body) . to_def_id ( ) ) == DefKind :: AnonConst {
431
+ // FIXME: Omit the curly braces if the enclosing expression is an array literal
432
+ // with a repeated element (an `ExprKind::Repeat`) as in such case it
433
+ // would not actually need any disambiguation.
434
+ "{ _ }" . to_owned ( )
435
+ } else {
436
+ "_" . to_owned ( )
437
+ }
357
438
}
358
439
359
440
/// Given a type Path, resolve it to a Type using the TyCtxt
0 commit comments