Skip to content

Error message for pointerof should be more helpful #15865

@straight-shoota

Description

@straight-shoota

When passing pointerof a value that doesn't have a pointer location, it errors with the message can't take address of #{expression}.

This is not very helpful because it requires an understanding of how pointerof works and what kind of expression it is supposed to receive. This can be challenging for newcomers (cf. https://forum.crystal-lang.org/t/sdl-event-and-userevent-questions/8114/6?u=straight-shoota).

Instead of only stating that taking an address of the given expression doesn't work, it could be useful to state what would work: The expression must be a variable (local, instance, class) or a constant.
Perhaps it would also be good to mention the type of the expression.

Here's a suggestion for rewording the message:

# old
pointerof(1) # Error: can't take address of 1

# new
pointerof(1) # Error: can't take address of `1` because it's a NumberLiteral. `pointerof` expects a variable or constant.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions