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

Multiple map key patterns raise an unexpected type check error #507

Closed
javiergarea opened this issue Jan 24, 2023 · 4 comments · Fixed by #509
Closed

Multiple map key patterns raise an unexpected type check error #507

javiergarea opened this issue Jan 24, 2023 · 4 comments · Fixed by #509
Assignees

Comments

@javiergarea
Copy link
Contributor

Current behaviour

Given the following spec and implementation:

-spec check(Input :: #{binary() => integer()}) -> integer() | error.
check(#{<<"a">> := A}) ->
    A;
check(#{<<"b">> := B}) ->
    B;
check(_Other) ->
    error.

gradualizer raises a The expression <<"b">> on line 13 at column 9 is not a valid key in the map type #{}.

Expected behaviour

No type check errors.

Notes

  • Minimal repo to reproduce the issue: here.
@javiergarea javiergarea changed the title Multiple key pattern matching in maps raises an unexpected type check error Multiple map key patterns raise an unexpected type check error Jan 24, 2023
@erszcz
Copy link
Collaborator

erszcz commented Jan 24, 2023

Thanks for raising this, @javiergarea!

This is a problem caused by refining a map key with a match-all binary() even though the matched pattern is just a singleton binary. With some print-debugging, we see it clearly:

$ ~/work/erszcz/gradualizer/bin/gradualizer src/gradualizer_maps.erl
assoc_tys: [{type,0,map_field_assoc,[{var,0,'_'},{type,0,integer,[]}]}]
assoc_tys: []
src/gradualizer_maps.erl: The expression <<"b">> on line 13 at column 9 is not a valid key in the map type #{}

I think we'll have to drop exhaustiveness checking on maps to solve this, at least in the short/mid term.

@zuiderkwast
Copy link
Collaborator

I agree with the analysis @erszcz. Since <<"a">> doesn't exhaust the key type binary(), the map type shouldn't be refined to the empty map for the next clause. I'm not sure how complicated this would be to fix though.

@zuiderkwast
Copy link
Collaborator

@javiergarea To show how to reproduce a problem, I recommend adding the example module under test/known_problems/. This is hopefully easier than setting up a separate application.

@javiergarea
Copy link
Contributor Author

@zuiderkwast You are totally right! #508

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 a pull request may close this issue.

3 participants