stricter skip for conversions in array indices in transf #24424
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fixes #17958
In
transf
, conversions in subscript expressions are skipped (withskipConv
's rules). This is because array indexing can produce conversions to the range type that is the array's index type, which causes aRangeDefect
rather than anIndexDefect
(and also--rangeChecks
and--indexChecks
are both considered). However this causes problems when explicit conversions are used, between types of different bitsizes, because those also get skipped.To fix this, we only skip the conversion if:
nkChckRange
)And
skipConv
rules also still apply (int/float classification).Another idea would be to prevent the implicit conversion to the array index type from being generated. But there is no good way to do this: matching to the base type instead prevents types like
uint32
from implicitly converting (i.e. it can convert torange[0..3]
but notint
), and analyzing whether this is an array bound check is easier intransf
, sincesigmatch
just produces a type conversion.The rules for skipping the conversion could also receive some other tweaks: We could add a rule that changing bitsizes also doesn't skip the conversion, but this breaks the
uint32
case. We could simplify it to only removing implicit skips to specifically fix #17958, but this is wrong in general.We could also add something like
nkChckIndex
that generates index errors instead but this is weird when it doesn't have access to the collection type and it might be overkill.