Skip to content

Commit 17b9285

Browse files
authored
Make using :A an error; fix using A: (..) warning (#477)
1 parent 3a12196 commit 17b9285

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/parser.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,6 +2403,8 @@ function parse_atsym(ps::ParseState, allow_quotes=true)
24032403
# export outer ==> (export outer)
24042404
# export ($f) ==> (export ($ f))
24052405
mark = position(ps)
2406+
# Syntax Edition TODO: make all the various ways to quote things inside
2407+
# import paths an error and require `var""` in the few remaining cases.
24062408
if allow_quotes && peek(ps) == K":" && !is_closing_token(ps, peek(ps,2))
24072409
# import A.:+ ==> (import (importpath A (quote-: +)))
24082410
emit_diagnostic(ps, warning="quoting with `:` is not required here")
@@ -2423,10 +2425,10 @@ function parse_atsym(ps::ParseState, allow_quotes=true)
24232425
warn_parens = true
24242426
end
24252427
end
2426-
if warn_parens
2428+
b = peek_behind(ps, pos)
2429+
if warn_parens && b.orig_kind != K".."
24272430
emit_diagnostic(ps, mark, warning="parentheses are not required here")
24282431
end
2429-
b = peek_behind(ps, pos)
24302432
ok = (b.is_leaf && (b.kind == K"Identifier" || is_operator(b.kind))) ||
24312433
(!b.is_leaf && b.kind in KSet"$ var")
24322434
if !ok
@@ -2497,7 +2499,7 @@ function parse_import(ps::ParseState, word, has_import_prefix)
24972499
# import A: x as y ==> (import (: (importpath A) (as (importpath x) y)))
24982500
# using A: x as y ==> (using (: (importpath A) (as (importpath x) y)))
24992501
bump(ps, TRIVIA_FLAG)
2500-
parse_atsym(ps)
2502+
parse_atsym(ps, false)
25012503
emit(ps, mark, K"as")
25022504
if word == K"using" && !has_import_prefix
25032505
# using A as B ==> (using (error (as (importpath A) B)))
@@ -2552,7 +2554,7 @@ function parse_import_path(ps::ParseState)
25522554
else
25532555
# import @x ==> (import (importpath @x))
25542556
# import $A ==> (import (importpath ($ A)))
2555-
parse_atsym(ps)
2557+
parse_atsym(ps, false)
25562558
end
25572559
while true
25582560
t = peek_token(ps)

test/diagnostics.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,15 @@ end
125125
Diagnostic(9, 9, :warning, "space between dots in import path")
126126
@test diagnostic("import A.:+") ==
127127
Diagnostic(10, 10, :warning, "quoting with `:` is not required here")
128-
# No warning for import `:` symbol
128+
# No warnings for imports of `:` and parenthesized `(..)`
129129
@test diagnostic("import A.:, :", allow_multiple=true) == []
130+
@test diagnostic("import A: (..)", allow_multiple=true) == []
130131
@test diagnostic("import A.(:+)") ==
131132
Diagnostic(10, 13, :warning, "parentheses are not required here")
132133
@test diagnostic("export (x)") ==
133134
Diagnostic(8, 10, :warning, "parentheses are not required here")
135+
@test diagnostic("import :A") ==
136+
Diagnostic(8, 9, :error, "expected identifier")
134137
@test diagnostic("export :x") ==
135138
Diagnostic(8, 9, :error, "expected identifier")
136139
@test diagnostic("public = 4", version=v"1.11") ==

test/parser.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,11 @@ tests = [
676676
"import A.⋆.f" => "(import (importpath A ⋆ f))"
677677
"import A..." => "(import (importpath A ..))"
678678
"import A; B" => "(import (importpath A))"
679+
# Colons not allowed first in import paths
680+
# but are allowed in trailing components (#473)
681+
"using :A" => "(using (importpath (error (quote-: A))))"
682+
"using A: :b" => "(using (: (importpath A) (importpath (error (quote-: b)))))"
683+
"using A: b.:c" => "(using (: (importpath A) (importpath b (quote-: c))))"
679684
],
680685
JuliaSyntax.parse_iteration_specs => [
681686
"i = rhs" => "(iteration (in i rhs))"

0 commit comments

Comments
 (0)