Module: SimpleCov::StaticCoverageExtractor::ConditionFolding

Included in:
Visitor
Defined in:
lib/simplecov/static_coverage_extractor/condition_folding.rb

Overview

Detects the if / unless / ternary conditions CRuby folds away. When a condition is a statically-known-truthy/falsy literal the compiler eliminates the dead arm and Coverage emits no branch, so the extractor must not synthesize one either: the arm would be a phantom no loaded run can hit, the same unmergeable-tuple failure as #1226 / #1233.

Constant Summary collapse

FOLDS_SOURCE_FILE =

CRuby 3.4 rebuilt the fold on the Prism compiler, and the parse.y fold it replaced still differs on 3.3, where __FILE__ folds.

Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.4")
CONTAINER_CONTENTS_NEED_STATIC_LITERALS =

Container literals in discarded position are eliminated from 3.3 on, but 3.3's compile.c elides a container whose contents are merely effect-free ([x]), while the Prism compiler demands fully static literals ([1] goes, [x] stays).

!FOLDS_SOURCE_FILE
STATIC_CONDITION_TYPES =

The literals that fold. while / until do NOT fold (while true is a real branch), so only the if-like visitors consult this. Regexp and Range literals are excluded on purpose: as conditions they mean =~ $_ / flip-flop, which Coverage does branch on. [], {}, and interpolated strings do not fold either, and -> folds while a lambda call does not: a method named lambda proves nothing. simplecov:disable branch — which arm runs is fixed by the running Ruby's version

[
  ::Prism::IntegerNode, ::Prism::FloatNode, ::Prism::RationalNode,
  ::Prism::ImaginaryNode, ::Prism::SymbolNode, ::Prism::StringNode,
  ::Prism::TrueNode, ::Prism::FalseNode, ::Prism::NilNode,
  ::Prism::SourceLineNode, ::Prism::SourceEncodingNode, ::Prism::LambdaNode,
  *(::Prism::SourceFileNode if FOLDS_SOURCE_FILE)
].freeze
FALSY_CONDITION_TYPES =

simplecov:enable branch

[::Prism::FalseNode, ::Prism::NilNode].freeze
PAREN_OPAQUE_TYPES =

CRuby folds if nil, if "x", and if -> {} but keeps a real branch for if (nil), if ("x"), and if (-> {}), while every other literal folds parenthesized or not.

[
  ::Prism::NilNode, ::Prism::StringNode, ::Prism::LambdaNode, ::Prism::SourceFileNode
].freeze
STATIC_LITERAL_LEAF_TYPES =

A multi-statement paren condition (if (1; 2)) folds by its last expression only when every leading statement is eliminated when discarded, and these always are, bare or composing an Array/Hash/Range.

[
  ::Prism::IntegerNode, ::Prism::FloatNode, ::Prism::RationalNode,
  ::Prism::ImaginaryNode, ::Prism::StringNode, ::Prism::SymbolNode,
  ::Prism::TrueNode, ::Prism::FalseNode, ::Prism::NilNode,
  ::Prism::SourceLineNode, ::Prism::SourceFileNode, ::Prism::SourceEncodingNode, ::Prism::RegularExpressionNode
].freeze
ELIMINABLE_READ_TYPES =

Non-literal reads that are also eliminated when discarded. Anything that can raise or run hooks is never eliminated and keeps the branch real.

[
  ::Prism::SelfNode, ::Prism::LocalVariableReadNode,
  ::Prism::InstanceVariableReadNode, ::Prism::DefinedNode
].freeze