Skip to content

Commit

Permalink
Ensure Crystal::Visitor#visit returns Bool (#14266)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Jan 30, 2024
1 parent 8a2a1a8 commit d88c013
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 11 deletions.
1 change: 1 addition & 0 deletions samples/compiler/visitor_example.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Counter < Crystal::Visitor

def visit(node : Crystal::NumberLiteral)
@count += 1
false
end

def visit(node : Crystal::ASTNode)
Expand Down
17 changes: 14 additions & 3 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -501,18 +501,22 @@ module Crystal

def visit(node : Nop)
@last = llvm_nil
false
end

def visit(node : NilLiteral)
@last = llvm_nil
false
end

def visit(node : BoolLiteral)
@last = int1(node.value ? 1 : 0)
false
end

def visit(node : CharLiteral)
@last = int32(node.value.ord)
false
end

def visit(node : NumberLiteral)
Expand Down Expand Up @@ -542,14 +546,17 @@ module Crystal
in .f64?
@last = float64(node.value)
end
false
end

def visit(node : StringLiteral)
@last = build_string_constant(node.value, node.value)
false
end

def visit(node : SymbolLiteral)
@last = int(@symbols[node.value])
false
end

def visit(node : TupleLiteral)
Expand Down Expand Up @@ -1092,7 +1099,7 @@ module Crystal

request_value(value)

return if value.no_returns?
return false if value.no_returns?

last = @last

Expand All @@ -1106,7 +1113,7 @@ module Crystal
read_class_var_ptr(target)
when Var
# Can't assign void
return if target.type.void?
return false if target.type.void?

# If assigning to a special variable in a method that yields,
# assign to that variable too.
Expand Down Expand Up @@ -1284,6 +1291,7 @@ module Crystal
else
node.raise "BUG: missing context var: #{node.name}"
end
false
end

def visit(node : Global)
Expand All @@ -1292,6 +1300,7 @@ module Crystal

def visit(node : ClassVar)
@last = read_class_var(node)
false
end

def visit(node : InstanceVar)
Expand Down Expand Up @@ -1403,7 +1412,7 @@ module Crystal

unless filtered_type
@last = upcast llvm_nil, resulting_type, @program.nil
return
return false
end

non_nilable_type = node.non_nilable_type
Expand Down Expand Up @@ -1664,6 +1673,7 @@ module Crystal

def visit(node : Unreachable)
builder.unreachable
false
end

def check_proc_is_not_closure(value, type)
Expand Down Expand Up @@ -2049,6 +2059,7 @@ module Crystal
def unreachable(file = __FILE__, line = __LINE__)
debug_codegen_log(file, line) { "Reached the unreachable!" }
builder.unreachable
false
end

def allocate_aggregate(type)
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/codegen/primitives.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Crystal::CodeGenVisitor
else
raise "BUG: unhandled primitive in codegen visit: #{node.name}"
end

false
end

def codegen_primitive(call, node, target_def, call_args)
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
# particularly when outside of a method.
if is_self && !scope.is_a?(Program) && !scope.passed_as_self?
put_type scope, node: node
return
return false
end

local_var = lookup_local_var_or_closured_var(node.name)
Expand Down Expand Up @@ -1687,7 +1687,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
if node.upcast?
upcast node.obj, obj_type, node.non_nilable_type
upcast node.obj, node.non_nilable_type, node.type
return
return false
end

# Check if obj is a `to_type`
Expand Down Expand Up @@ -3158,6 +3158,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
{% end %}

call compiled_def, node: node
false
end

def visit(node : ASTNode)
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ module Crystal

def visit(node : MacroLiteral)
@str << node.value
false
end

def visit(node : MacroVerbatim)
Expand All @@ -135,7 +136,8 @@ module Crystal
def visit(node : Var)
var = @vars[node.name]?
if var
return @last = var
@last = var
return false
end

# Try to consider the var as a top-level macro call.
Expand All @@ -150,7 +152,8 @@ module Crystal
# and in this case the parser has no idea about this, so the only
# solution is to do it now.
if value = interpret_top_level_call?(Call.new(nil, node.name))
return @last = value
@last = value
return false
end

node.raise "undefined macro variable '#{node.name}'"
Expand Down Expand Up @@ -549,6 +552,7 @@ module Crystal
else
node.raise "unknown macro instance var: '#{node.name}'"
end
false
end

def visit(node : TupleLiteral)
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/semantic/cleanup_transformer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,12 @@ module Crystal
if @a_def.vars.try &.[node.name]?.try &.closured?
@vars << node
end
false
end

def visit(node : InstanceVar)
@vars << node
false
end

def visit(node : ASTNode)
Expand Down
22 changes: 19 additions & 3 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ module Crystal
node.syntax_replacement = type
node.bind_to type
end

false
end

def visit(node : Generic)
Expand Down Expand Up @@ -353,6 +355,7 @@ module Crystal

def visit(node : Self)
node.type = the_self(node).instance_type
false
end

def visit(node : Var)
Expand Down Expand Up @@ -394,6 +397,7 @@ module Crystal
else
node.raise "read before assignment to local variable '#{node.name}'"
end
false
end

def visit(node : TypeDeclaration)
Expand Down Expand Up @@ -628,6 +632,8 @@ module Crystal
ivar.nil_reason ||= NilReason.new(node.name, :used_before_initialized, [node] of ASTNode)
ivar.bind_to program.nil_var
end

false
end

def visit(node : ReadInstanceVar)
Expand Down Expand Up @@ -1007,7 +1013,7 @@ module Crystal
end

def visit(node : Block)
return if node.visited?
return false if node.visited?

node.visited = true
node.context = current_non_block_context
Expand Down Expand Up @@ -1630,6 +1636,7 @@ module Crystal
ivars[node.name] = node_in_callstack(node)
end
end
false
end

def visit(node : Var)
Expand Down Expand Up @@ -1761,7 +1768,7 @@ module Crystal
comp.accept self
node.syntax_replacement = comp
node.bind_to comp
return
return false
end

if needs_type_filters? && (var = get_expression_var(node.obj))
Expand Down Expand Up @@ -2060,7 +2067,7 @@ module Crystal
unless node.has_breaks?
if endless_while
node.type = program.no_return
return
return false
end

filter_vars TypeFilters.not(cond_type_filters)
Expand Down Expand Up @@ -2325,6 +2332,8 @@ module Crystal
else
node.raise "BUG: unhandled primitive in MainVisitor: #{node.name}"
end

false
end

def visit_va_arg(node)
Expand Down Expand Up @@ -3042,31 +3051,38 @@ module Crystal

def visit(node : Nop)
node.type = @program.nil
false
end

def visit(node : NilLiteral)
node.type = @program.nil
false
end

def visit(node : BoolLiteral)
node.type = program.bool
false
end

def visit(node : NumberLiteral)
node.type = program.type_from_literal_kind node.kind
false
end

def visit(node : CharLiteral)
node.type = program.char
false
end

def visit(node : SymbolLiteral)
node.type = program.symbol
program.symbols.add node.value
false
end

def visit(node : StringLiteral)
node.type = program.string
false
end

def visit(node : RegexLiteral)
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/semantic/restrictions_augmenter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ module Crystal

def visit(node : Call)
if expanded = node.expanded
return expanded.accept self
expanded.accept self
return false
end

node.obj.try &.accept self
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/crystal/semantic/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ module Crystal
def visit(node : Primitive)
@str << "# primitive: "
@str << node.name
false
end

def visit(node : MetaVar)
@str << node.name
false
end

def visit(node : MetaMacroVar)
@str << node.name
false
end

def visit(node : TypeFilteredNode)
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
typed_def_type = lookup_type(node.type_spec)
typed_def_type = check_allowed_in_lib node.type_spec, typed_def_type
current_type.types[node.name] = TypeDefType.new @program, current_type, node.name, typed_def_type
false
end
end

Expand Down
3 changes: 3 additions & 0 deletions src/compiler/crystal/semantic/type_guess_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module Crystal
end

check_var_is_self(node)
false
end

def visit(node : UninitializedVar)
Expand Down Expand Up @@ -114,6 +115,7 @@ module Crystal
# TODO: can this be reached?
end
end
false
end

def visit(node : Assign)
Expand Down Expand Up @@ -1353,6 +1355,7 @@ module Crystal
if node.name == "self"
@has_self = true
end
false
end

def visit(node : ASTNode)
Expand Down
Loading

0 comments on commit d88c013

Please sign in to comment.