Skip to content

Commit

Permalink
Compiler: fix crash when formatting syntax error inside macro (#8055)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and Brian J. Cardiff committed Aug 12, 2019
1 parent 80d9252 commit 4a0ca22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
8 changes: 8 additions & 0 deletions spec/compiler/semantic/macro_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1427,4 +1427,12 @@ describe "Semantic: macro" do
),
"can't instantiate abstract class Foo"
end

it "doesn't crash on syntax error inside macro (regression, #8038)" do
expect_raises(Crystal::SyntaxException, "unterminated array literal") do
semantic(%(
{% begin %}[{% end %}
))
end
end
end
25 changes: 15 additions & 10 deletions src/compiler/crystal/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -242,22 +242,27 @@ module Crystal

def minimize_indentation(source)
min_leading_white_space =
source.map do |line|
if match = line.match(/^(\s+)\S/)
spaces = match[1]?
spaces.size if spaces
end
end
.compact
.min
source.min_of? { |line| leading_white_space(line) } || 0

source = source.map do |line|
replace_leading_tabs_with_spaces(line).lchop(" " * min_leading_white_space)
if min_leading_white_space > 0
source = source.map do |line|
replace_leading_tabs_with_spaces(line).lchop(" " * min_leading_white_space)
end
end

{source, min_leading_white_space}
end

private def leading_white_space(line)
match = line.match(/^(\s+)\S/)
return 0 unless match

spaces = match[1]?
return 0 unless spaces

spaces.size
end

def append_expanded_macro(io, source)
line_number = @line_number
if @error_trace || !line_number
Expand Down

0 comments on commit 4a0ca22

Please sign in to comment.