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
Invalid suggestion for psalm --alter
and strange const error
#7973
Comments
Hey @ricardoboss, can you reproduce the issue on https://psalm.dev ? |
Yeah, I can reproduce it... but why? https://psalm.dev/r/d757d80ca9 |
I found these snippets: https://psalm.dev/r/d757d80ca9<?php
interface Duration
{
public const SECONDS_PER_MINUTE = 60;
public const SECONDS_PER_HOUR = self::SECONDS_PER_MINUTE * self::MINUTES_PER_HOUR;
public const SECONDS_PER_DAY = self::SECONDS_PER_HOUR * self::HOURS_PER_DAY;
// THE LINE BELOW causes the error?
public const SECONDS_PER_MONTH = self::SECONDS_PER_DAY * self::DAYS_PER_MONTH;
public const MINUTES_PER_HOUR = 60;
public const HOURS_PER_DAY = 24;
public const DAYS_PER_MONTH = self::DAYS_PER_YEAR / self::MONTHS_PER_YEAR;
public const DAYS_PER_YEAR = 365.2425; // average length of a Gregorian year over a complete leap cycle of 400 years
public const MONTHS_PER_YEAR = 12;
}
|
Also, if I add:
to the class, it still reports the errors. Is this supposed to work? It also doesn't work if I add it to each individual const, reporting each as unused next to the error I want to suppress. |
@AndrolGenhald That's weird, seems like the property is analyzed twice with two different values for the calculation |
Yeah, I've run into this before but most of the issues were fixed. I think the problem is that it's being resolved once by |
If it helps: the order matters! Moving the |
As does moving |
Apart from the consts, what about the |
I'm not sure what's going on there, I don't think that issue should even be showing up as fixable. I'll look into that as well. |
Turns out the issue is in BinaryOpAnalyzer, it calculates literal expressions for ints but not for floats. The constant type inferrer gets the literal type when determining the constant's type, but then when the statement is later analyzed to make sure the assignment matches the constant's type it doesn't calculate the literal type, so it doesn't match. Working on a fix. |
vimeo#7973). - Calculate literal type for float arithmetic instead of only for int arithmetic - Fix copy/paste fail causing InvalidConstantAssignmentValue to be marked as fixable
Fix class const issue when using floats declared in future consts (fixes #7973).
There's a lot to unpack here... Any idea why any of this is happening? I don't understand the error message (I have not declared any type for the const, it should infer the type from the value) and the
--alter
suggestion doesn't even work.The text was updated successfully, but these errors were encountered: