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

Warn on suffix-less integer literals outside Int64's range #12427

Conversation

HertzDevil
Copy link
Contributor

Implements #8373 (comment). The warnings are generated in a few places:

  • when a source is compiled, formatted, or interpreted
  • whenever a macro is interpolated and new code is to be parsed
  • when a line of source is accepted from an interpreter session
  • when the macro method parse_type returns (because generic type arguments may contain large integer literals)

The formatter could technically swallow the warnings and append u64 to all the large literals, which is actually what I did with the last commit here:

class Crystal::Formatter
  def visit(node : NumberLiteral)
    if @token.type.magic_line?
      write @token.type
      next_token
      return false
    end

    check :NUMBER
    write @token.raw
    if @token.number_kind.u64? && !@token.raw.ends_with?("u64") && !node.value.to_i64?
      write '_' if @token.raw.includes?('_')
      write "u64"
    end
    next_token

    false
  end
end

Some places like the compiler-rt specs add u64 to a lot of literals regardless of whether they actually require one, because they look better.

@HertzDevil HertzDevil force-pushed the feature/warn-large-uint64-literals branch from 47bfc14 to 51ab182 Compare August 30, 2022 10:50
@HertzDevil HertzDevil marked this pull request as draft August 30, 2022 14:00
@HertzDevil HertzDevil marked this pull request as ready for review September 2, 2022 02:04
@straight-shoota straight-shoota added this to the 1.6.0 milestone Sep 2, 2022
@straight-shoota straight-shoota merged commit 8b801a5 into crystal-lang:master Sep 5, 2022
@HertzDevil HertzDevil deleted the feature/warn-large-uint64-literals branch September 6, 2022 01:46
potomak added a commit to potomak/crystal that referenced this pull request Sep 21, 2022
I've turned failures to parse params with invalid names into syntax
warnings similar to crystal-lang#12427, as suggested by @HertzDevil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants