Skip to content

Conversation

@kalexmills
Copy link
Contributor

@kalexmills kalexmills commented Apr 7, 2025

This PR uses Go's type checker to compute and use constant expressions everywhere.

This will close #32 and will additionally support arbitrary numerical calculations when -numbers is set.

It will break compatibility with golangci-lint, as goconst will now require type information to be used. We'll need a PR upstream to plumb it through.

This PR ignores type checking errors in several locations. This is because the type checker checks a lot of extraneous information that we do not care about. The type checker can typically determine the value and type of constant expressions even if other errors are found in the file. In case type checking totally fails, treeVisitor falls back on its prior behavior.

Note: Since this PR casts numbers to strings prior to checking for collisions, we should have a lot more tests for various kinds of large numbers, and especially tests to check different floating point representations.

@kalexmills kalexmills marked this pull request as draft April 7, 2025 00:35
@codecov
Copy link

codecov bot commented Apr 7, 2025

Codecov Report

Attention: Patch coverage is 74.57627% with 45 lines in your changes missing coverage. Please review.

Please upload report for BASE (master@0244082). Learn more about missing BASE report.

Files with missing lines Patch % Lines
parser.go 72.72% 39 Missing and 3 partials ⚠️
visitor.go 81.25% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master      #35   +/-   ##
=========================================
  Coverage          ?   65.66%           
=========================================
  Files             ?        4           
  Lines             ?     1031           
  Branches          ?        0           
=========================================
  Hits              ?      677           
  Misses            ?      321           
  Partials          ?       33           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalexmills kalexmills marked this pull request as ready for review April 7, 2025 00:45
@kalexmills kalexmills changed the title Compute and use constant expressions. Compute and use constant expressions via the type checker. Apr 7, 2025
@kalexmills
Copy link
Contributor Author

@jgautheron Let's rebase this on your branch so we can keep the tests and CLI changes. I should have time to do so this evening, but feel free to do it sooner if it's blocking anything.

@kalexmills kalexmills force-pushed the kalexmills/const-expr branch from 3c64043 to 78ef201 Compare April 7, 2025 23:04
@kalexmills kalexmills force-pushed the kalexmills/const-expr branch from 78ef201 to 6b10de8 Compare April 7, 2025 23:28
@kalexmills
Copy link
Contributor Author

kalexmills commented Apr 8, 2025

@jgautheron here is a combination of both our branches which ought to be able to go in without breaking anything.

@jgautheron
Copy link
Owner

Nice work, lgtm!

@jgautheron jgautheron merged commit af8760d into jgautheron:master Apr 8, 2025
7 checks passed
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.

Support for constant expressions

2 participants