Skip to content

Improve error when indexing is interpreted as a typed comprehension#49939

Merged
LilithHafner merged 7 commits intoJuliaLang:masterfrom
romaindegivry:typed_hcat-trap
May 26, 2023
Merged

Improve error when indexing is interpreted as a typed comprehension#49939
LilithHafner merged 7 commits intoJuliaLang:masterfrom
romaindegivry:typed_hcat-trap

Conversation

@romaindegivry
Copy link
Copy Markdown
Contributor

When indexing into an array, writing vec[i +1] is interpreted as a typed comprehension vec[i 1]. Because vec is not a type, it causes a unhelpful error.

This adds a new error message for this case. Example:

julia> vec = [1 2 3]; vec[1 +1]
ERROR: ArgumentError: First argument must be a type. Hint: if indexing, missing or misplaced operators can cause this (e.g. vec[i +1] instead of vec[i+1]).

This is my first PR here, please let me know if I can make any improvements.

See #49676

This adds a special error for indexing that gets resolved as a typed comprehension.
Copy link
Copy Markdown
Member

@LilithHafner LilithHafner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making a PR! I left some minor comments, but this looks like a good implementation design and it is already better than what we have in 1.9 and master.

Comment thread base/abstractarray.jl Outdated
Comment thread test/abstractarray.jl Outdated
Comment thread base/abstractarray.jl Outdated
@romaindegivry
Copy link
Copy Markdown
Contributor Author

romaindegivry commented May 24, 2023

Thanks for your comments. Requested changes are addressed.

Copy link
Copy Markdown
Member

@LilithHafner LilithHafner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests look good to me and the error hint, while long, seems reasonable.

Comment thread base/abstractarray.jl Outdated
Suggested change during review

Co-authored-by: Lilith Orion Hafner <[email protected]>
Comment thread base/abstractarray.jl Outdated
Copy link
Copy Markdown
Member

@LilithHafner LilithHafner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely! Thank you so much :) I'll merge this tomorrow unless @jishnub or someone else objects.

Comment thread base/abstractarray.jl Outdated
# interpreted as a typed concatenation. (issue #49676)
typed_hcat(::AbstractArray, other...) = throw(ArgumentError("It is unclear whether you \
intend to perform an indexing operation or typed concatenation. If you intend to \
perform indexing (v[1 + 2]) insert missing operator or adjust spacing to clarify. \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor suggestion: it'll be easier to read if there's a comma after perform indexing (v[1 + 2]) and similarly after typed concatenation (T[1 2]). I'd also move adjust spacing before insert missing operator, as this is probably the common tripping hazard here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Updated.

@jishnub
Copy link
Copy Markdown
Member

jishnub commented May 25, 2023

Looks good to me, I've left a minor comment. Thanks for the PR!

@LilithHafner LilithHafner linked an issue May 26, 2023 that may be closed by this pull request
@LilithHafner LilithHafner merged commit fb3df69 into JuliaLang:master May 26, 2023
@LilithHafner
Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vec[1 +1] gives unhelpful error

3 participants