Class: Rigor::AST::TypeNode

Inherits:
Object
  • Object
show all
Includes:
Node
Defined in:
lib/rigor/ast/type_node.rb

Overview

A virtual node that wraps a Rigor::Type. Allows callers to ask “what would the analyzer infer at this position if the value’s type were T?” without constructing a real Prism expression.

Rigor::Scope#type_of(TypeNode.new(t)) MUST return a structurally- equal t. The engine MUST NOT modify or annotate the wrapped type.

Inspired by PHPStan’s TypeExpr (a synthetic Expr that returns a specific Type from $scope->getType). The Rigor counterpart is spelled “TypeNode” to align with Prism’s “Node” suffix convention.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ TypeNode

Returns a new instance of TypeNode.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/rigor/ast/type_node.rb', line 20

def initialize(type)
  raise ArgumentError, "TypeNode requires a non-nil Rigor::Type" if type.nil?

  @type = type
  freeze
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/rigor/ast/type_node.rb', line 18

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



27
28
29
# File 'lib/rigor/ast/type_node.rb', line 27

def ==(other)
  other.is_a?(TypeNode) && type == other.type
end

#hashObject



32
33
34
# File 'lib/rigor/ast/type_node.rb', line 32

def hash
  [TypeNode, type].hash
end

#inspectObject



36
37
38
# File 'lib/rigor/ast/type_node.rb', line 36

def inspect
  "#<Rigor::AST::TypeNode #{type.describe(:short)}>"
end