Class: Rigor::AST::TypeNode

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

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.

Parameters:

  • type (Type::t)

Raises:

  • (ArgumentError)


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

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

  @type = type
  freeze
end

Instance Attribute Details

#typeType::t (readonly)

Returns the value of attribute type.

Returns:

  • (Type::t)


16
17
18
# File 'lib/rigor/ast/type_node.rb', line 16

def type
  @type
end

Instance Method Details

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

Parameters:

  • other (Object)

Returns:

  • (Boolean)


25
26
27
# File 'lib/rigor/ast/type_node.rb', line 25

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

#hashInteger

Returns:

  • (Integer)


30
31
32
# File 'lib/rigor/ast/type_node.rb', line 30

def hash
  [TypeNode, type].hash
end

#inspectString

Returns:

  • (String)


34
35
36
# File 'lib/rigor/ast/type_node.rb', line 34

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