rustc_metadata::rmeta::encoder

Function rendered_const

Source
pub fn rendered_const<'tcx>(
    tcx: TyCtxt<'tcx>,
    body: &Body<'_>,
    def_id: LocalDefId,
) -> String
Expand description

Build a textual representation of an unevaluated constant expression.

If the const expression is too complex, an underscore _ is returned. For const arguments, it’s { _ } to be precise. This means that the output is not necessarily valid Rust code.

Currently, only

  • literals (optionally with a leading -)
  • unit ()
  • blocks ({ … }) around simple expressions and
  • paths without arguments

are considered simple enough. Simple blocks are included since they are necessary to disambiguate unit from the unit type. This list might get extended in the future.

Without this censoring, in a lot of cases the output would get too large and verbose. Consider match expressions, blocks and deeply nested ADTs. Further, private and doc(hidden) fields of structs would get leaked since HIR datatypes like the body parameter do not contain enough semantic information for this function to be able to hide them – at least not without significant performance overhead.

Whenever possible, prefer to evaluate the constant first and try to use a different method for pretty-printing. Ideally this function should only ever be used as a fallback.