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

Code printing of illegal identifiers, e.g. var"do" #56936

Open
StefanKarpinski opened this issue Jan 2, 2025 · 6 comments
Open

Code printing of illegal identifiers, e.g. var"do" #56936

StefanKarpinski opened this issue Jan 2, 2025 · 6 comments
Labels
display and printing Aesthetics and correctness of printed representations of objects. good first issue Indicates a good issue for first-time contributors to Julia help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@StefanKarpinski
Copy link
Member

The first is expected, the latter two are a bug and should be printed as they are input:

julia> :(var"x" = 1)
:(x = 1)

julia> :(var"do" = 1)
:(do = 1)

julia> :(var"end" = 1)
:(end = 1)
@StefanKarpinski StefanKarpinski added display and printing Aesthetics and correctness of printed representations of objects. help wanted Indicates that a maintainer wants help on an issue or pull request good first issue Indicates a good issue for first-time contributors to Julia labels Jan 2, 2025
@Abhishek022001
Copy link

can i work on this issue?

@mcabbott
Copy link
Contributor

mcabbott commented Jan 2, 2025

Also for NamedTuples:

julia> (; var"x" = 1, var"do" = 2, var"end" = 3)
(x = 1, do = 2, end = 3)

julia> (x = 1, do = 2, end = 3)
ERROR: ParseError:

@simeonschaub
Copy link
Member

One potential issue is that end is a valid identifier in certain contexts such as indexing expressions, it would be ugly to print it as var"end" in these cases. Would need some special-casing to make this work nicely in that instance.

can i work on this issue?

You don't need to ask, if the issue is still open, it's up for grabs

@inkydragon
Copy link
Member

Seems related:

julia/base/show.jl

Lines 1462 to 1471 in ed2cb49

## Abstract Syntax Tree (AST) printing ##
# Summary:
# print(io, ex) defers to show_unquoted(io, ex)
# show(io, ex) defers to show_unquoted(io, QuoteNode(ex))
# show_unquoted(io, ex) does the heavy lifting
#
# AST printing should follow two rules:
# 1. Meta.parse(string(ex)) == ex
# 2. eval(Meta.parse(repr(ex))) == ex

julia> ex = :((var"x" = 1, b = 2, c = 3))
:((x = 1, b = 2, c = 3))

julia> Meta.parse(string(ex)) == ex
true

julia> eval(Meta.parse(repr(ex))) == ex
true


julia> ex = :((; var"x" = 1, var"do" = 2, var"end" = 3))
:((; x = 1, do = 2, end = 3))

julia> Meta.parse(string(ex)) == ex
ERROR: ParseError:
# Error @ none:1:11
(; x = 1, do = 2, end = 3)
#         └┘ ── invalid `do` syntax
Stacktrace:
...

julia> eval(Meta.parse(repr(ex))) == ex
ERROR: ParseError:
# Error @ none:1:13
:((; x = 1, do = 2, end = 3))
#           └┘ ── invalid `do` syntax
Stacktrace:
...

@martinholters
Copy link
Member

One potential issue is that end is a valid identifier in certain contexts such as indexing expressions

It's arguably a quirk of parsing/lowering that end and var"end" are not distinguished at the Expr level there.

julia> let var"end"=1; (1:10)[var"end"]; end
10 # anything else than "end", and you get 1

@inkydragon
Copy link
Member

Seems a new bug?

julia> isdefined(Main, :end)
false

julia> print(var"end")
ERROR: UndefVarError: `end` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> (1:3)[var"end"]
3

julia> (1:3)[var"let"]
ERROR: UndefVarError: `let` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

julia> var"let" = 2
2

julia> (1:3)[var"let"]
2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects. good first issue Indicates a good issue for first-time contributors to Julia help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

6 participants