Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler: super is only special if it doesn't have a receiver #10011

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions spec/compiler/codegen/super_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,19 @@ describe "Codegen: super" do
(Foo || Bar).bar(true)
))
end

it "calls super on an object (#10004)" do
run(%(
class Foo
@foo = 42

def super
@foo
end

end

Foo.new.super
)).to_i.should eq(42)
end
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Crystal::CodeGenVisitor
return false
end

owner = node.name == "super" ? node.scope : node.target_def.owner
owner = node.super? ? node.scope : node.target_def.owner

call_args, has_out = prepare_call_args node, owner

Expand Down Expand Up @@ -357,7 +357,7 @@ class Crystal::CodeGenVisitor
call.uses_with_scope = node.uses_with_scope?
call.name_location = node.name_location

is_super = node.name == "super"
is_super = node.super?

with_cloned_context do
context.vars = new_vars
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/crystal/semantic/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Crystal::Call
matches = program_matches unless program_matches.empty?
end

if matches.empty? && owner.class? && owner.abstract? && name != "super"
if matches.empty? && owner.class? && owner.abstract? && !super?
matches = owner.virtual_type.lookup_matches(signature, analyze_all: with_literals)
end

Expand Down Expand Up @@ -1138,4 +1138,12 @@ class Crystal::Call
end
end
end

def super?
!obj && name == "super"
end

def previous_def?
!obj && name == "previous_def"
end
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/call_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Crystal::Call
raise_matches_not_found_for_virtual_metaclass_new owner
end

if name == "super"
if super?
defs = owner.lookup_defs_without_parents(def_name)
else
defs = owner.lookup_defs(def_name)
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1461,9 +1461,9 @@ module Crystal
# If it's a super or previous_def call inside an initialize we treat
# set instance vars from superclasses to not-nil.
def check_super_or_previous_def_in_initialize(node)
if @is_initialize && !node.obj && (node.name == "super" || node.name == "previous_def")
if @is_initialize && (node.super? || node.previous_def?)
all_vars = scope.all_instance_vars.keys
all_vars -= scope.instance_vars.keys if node.name == "super"
all_vars -= scope.instance_vars.keys if node.super?
all_vars.each do |name|
instance_var = scope.lookup_instance_var(name)

Expand Down Expand Up @@ -1730,15 +1730,15 @@ module Crystal
end
end

if node.name == "super"
if node.super?
@in_super += 1
end

true
end

def end_visit(node : Call)
if node.name == "super"
if node.super?
@in_super -= 1
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/normalizer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ module Crystal

def transform(node : Call)
# Copy enclosing def's args to super/previous_def without parenthesis
case node.name
when "super", "previous_def"
case node
when .super?, .previous_def?
if node.args.empty? && !node.has_parentheses?
if current_def = @current_def
splat_index = current_def.splat_index
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/semantic_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ abstract class Crystal::SemanticVisitor < Crystal::Visitor
the_macro = macro_scope.metaclass.lookup_macro(node.name, node.args, node.named_args)
node.raise "private macro '#{node.name}' called for #{obj}" if the_macro.is_a?(Macro) && the_macro.visibility.private?
when Nil
return false if node.name == "super" || node.name == "previous_def"
return false if node.super? || node.previous_def?
the_macro = node.lookup_macro
else
return false
Expand Down