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 differed in three observable ways: __FILE__ folded on 3.2/3.3; parentheses were transparent for every literal on 3.2; and on 3.2 the dead arm's branch table entries survive the fold while its defs still never register.

Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.4")
PARENS_ALWAYS_TRANSPARENT =
Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.3")
DEAD_ARM_BRANCHES_SURVIVE =
PARENS_ALWAYS_TRANSPARENT
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. Consulted only when parentheses are not always transparent.

[
  ::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
PRISM_ERA_ELIMINABLE_READS =

self is eliminated by every supported compiler; local/ivar/defined? elimination arrived with the Prism-era compilers. Anything that can raise or run hooks is never eliminated and keeps the branch real.

[
  ::Prism::LocalVariableReadNode, ::Prism::InstanceVariableReadNode, ::Prism::DefinedNode
].freeze
ELIMINABLE_READ_TYPES =

simplecov:disable branch — which arm runs is fixed by the running Ruby's version

[
  ::Prism::SelfNode,
  *(PRISM_ERA_ELIMINABLE_READS unless PARENS_ALWAYS_TRANSPARENT)
].freeze