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

Fix name locations of FunDef and External nodes #14267

Merged
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
18 changes: 18 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,24 @@ module Crystal
name_location.column_number.should eq(12)
end

it "sets location of top-level fun name" do
parser = Parser.new("fun foo; end")
node = parser.parse.as(FunDef)

name_location = node.name_location.should_not be_nil
name_location.line_number.should eq(1)
name_location.column_number.should eq(5)
end

it "sets location of lib fun name" do
parser = Parser.new("lib Foo; fun foo; end")
node = parser.parse.as(LibDef).body.as(FunDef)

name_location = node.name_location.should_not be_nil
name_location.line_number.should eq(1)
name_location.column_number.should eq(14)
end

it "sets correct location of proc literal" do
parser = Parser.new("->(\n x : Int32,\n y : String\n) { }")
node = parser.parse.as(ProcLiteral)
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 @@ -918,6 +918,7 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
end

external = External.new(node.name, external_args, node.body, node.real_name).at(node)
external.name_location = node.name_location

call_convention = nil
process_def_annotations(external, annotations) do |annotation_type, ann|
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,7 @@ module Crystal
property real_name : String
property doc : String?
property? varargs : Bool
property name_location : Location?

def initialize(@name, @args = [] of Arg, @return_type = nil, @varargs = false, @body = nil, @real_name = name)
end
Expand All @@ -1965,7 +1966,13 @@ module Crystal
end

def clone_without_location
FunDef.new(@name, @args.clone, @return_type.clone, @varargs, @body.clone, @real_name)
clone = FunDef.new(@name, @args.clone, @return_type.clone, @varargs, @body.clone, @real_name)
clone.name_location = name_location
clone
end

def name_size
@name.size
end

def_equals_and_hash @name, @args, @return_type, @varargs, @body, @real_name
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5732,6 +5732,7 @@ module Crystal
with_isolated_var_scope(require_body) do
next_token_skip_space_or_newline

name_location = @token.location
name = if top_level
check_ident
else
Expand Down Expand Up @@ -5835,6 +5836,7 @@ module Crystal
end

fun_def = FunDef.new name, params, return_type, varargs, body, real_name
fun_def.name_location = name_location
fun_def.doc = doc
fun_def.at(location).at_end(end_location)
end
Expand Down
Loading