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

beam_validator: Track the unit of saved bs match positions #2744

Conversation

jhogberg
Copy link
Contributor

@jhogberg jhogberg commented Sep 8, 2020

@jhogberg jhogberg self-assigned this Sep 8, 2020
@jhogberg jhogberg added fix team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI labels Sep 8, 2020
@jhogberg jhogberg changed the base branch from maint-23 to maint September 8, 2020 14:02

ok.

bgbct_1(<<Bin/binary>> = Bin) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't something like this be rejected by the linter in the first place, though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could warn about it from OTP 24 onwards. As iffy as this seems it's still legal, and I'd rather not change it in a patch.

Copy link
Contributor

@jechol jechol Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm original reporter of this issue for Elixir (elixir-lang/elixir#10309) and I want explain why I happened to write code like that.

I need this expression to include argument name bin in documents generated by ex_doc.
If I only include <<bin::binary>>, then argument in document is named as arg.
And I prefer <<bin::binary>> pattern matching to is_binary(bin) guard.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jechol Would it work for the documentation if you would write the function head like this?

def encode_bin(<<_::binary>> = bin) do

The compiler will generate efficient code for that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjorng That's what I want. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was specifically referring to variables on the left & right of patterns being "compared". I don't think it can ever succeed beyond this special case in binary where the pattern match is a no-op. Everywhere else a part of a data structure can't be equal to the data structure itself, e.g. {X, _} = X shouldn't even compile as it will always fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalmuskala I think that {X, _} = X should produce a warning. That is what we do for other unmatchable patterns such as [X,Y] = {X,Y}. Our reasoning is that macros, parse transformations and naive code generators could produce nonsensical but working code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing a warning in such cases would be a good improvement, agreed.

@jhogberg jhogberg removed the testing currently being tested, tag is used by OTP internal CI label Sep 8, 2020
@jhogberg jhogberg force-pushed the john/compiler/fix-bs-validation-error/ERL-1340/OTP-16838 branch from f092dde to db7ab57 Compare September 8, 2020 20:22
@jhogberg jhogberg changed the title beam_validator: Fix type inference for binary tail extraction beam_validator: Track the unit of saved bs match positions Sep 8, 2020
@jhogberg jhogberg force-pushed the john/compiler/fix-bs-validation-error/ERL-1340/OTP-16838 branch from db7ab57 to b6ba7f7 Compare September 8, 2020 20:41
@jhogberg jhogberg requested a review from bjorng September 8, 2020 20:44
@jhogberg jhogberg force-pushed the john/compiler/fix-bs-validation-error/ERL-1340/OTP-16838 branch from b6ba7f7 to 3556c9d Compare September 8, 2020 21:57
lib/compiler/src/beam_validator.erl Show resolved Hide resolved
lib/compiler/test/beam_validator_SUITE.erl Outdated Show resolved Hide resolved
Prior to this commit, the unit of a match context would remain
unchanged when rewinding, which would fail validation at best and
let bugs slip by at worst.

After fixing this it became apparent that unit updates on
successful matches were broken as well, providing narrower types
than they should have, so we'll fix that in this commit too to
avoid breaking bisect for everyone.
@jhogberg jhogberg force-pushed the john/compiler/fix-bs-validation-error/ERL-1340/OTP-16838 branch from 3556c9d to 5474277 Compare September 11, 2020 08:01
@jhogberg jhogberg added the testing currently being tested, tag is used by OTP internal CI label Sep 11, 2020
@jhogberg jhogberg merged commit 82ea77f into erlang:maint Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants