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

Warning emitted about type variable being used only once when it is used more than once #8533

Closed
lpil opened this issue May 31, 2024 · 13 comments
Assignees
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM

Comments

@lpil
Copy link
Contributor

lpil commented May 31, 2024

Describe the bug

Hello! I have typespecs that feature a type variable used multiple times, but OTP27 is emitting a warning saying they are used only once.

$ erlc wibble.erl
foo.erl:5:27: Warning: type variable 'X' is only used once (is unbound)
%    5| -spec wibble(atom()) -> {ok, X} | {error, X}.
%     |                           ^

To Reproduce

Compile a module with a function with this typespec:

-spec wibble(atom()) -> {ok, X} | {error, X}.

Expected behavior

Any resulting warning not to say the type variable is only used once.

OR

No warning to be omitted as the type variable to be used twice

Affected versions

OTP 27.0

OTP 26.2.5 does not emit a warning here.

Thanks,
Louis

@lpil lpil added the bug Issue is reported as a bug label May 31, 2024
@lpil lpil changed the title Warning emitted about type variable being used once when it is used more than once Warning emitted about type variable being used only once when it is used more than once May 31, 2024
@dvic
Copy link

dvic commented May 31, 2024

OTP 26.2.5 does not emit a warning here.

Just to avoid any confusion: I pointed @lpil in the wrong direction, OTP 26.2.5 actually does emit a warning, it's OTP < 26 (I tested 25.3.2.12) that does not emit a warning.

@essen
Copy link
Contributor

essen commented May 31, 2024

This is intentional. See #6915 for a previous ticket with links and/or explanations.

@lpil
Copy link
Contributor Author

lpil commented May 31, 2024

Aye! I'm suggesting that the warning message is confusing or possibly incorrect? It says that the type variable is used once, but it is used twice in this case.

@jhogberg
Copy link
Contributor

It isn't used twice, though: each of those is a separate X that happens to share the same name. In other languages (e.g. C or Rust) it would be analogous to having variables declared in two different blocks with the same name.

@lpil
Copy link
Contributor Author

lpil commented May 31, 2024

I see! I presumed it worked like value variables did in Erlang. What's the syntax for having the same variable used twice?

@jhogberg
Copy link
Contributor

{X, X} would use it twice. Disjunctions add (inner) scopes, so {X, {a, X} | b} should also work without a warning.

@lpil
Copy link
Contributor Author

lpil commented Jun 1, 2024

What's the correct syntax for the typespec I have here? Both X should stand in for the same type.

-spec wibble(atom()) -> {ok, X} | {error, X}.

@jhogberg
Copy link
Contributor

jhogberg commented Jun 2, 2024

There is no way to express that. :/

@essen
Copy link
Contributor

essen commented Jun 2, 2024

I do

-spec wibble(atom()) -> {ok, X} | {error, X} when X::type().

Because even if Dialyzer doesn't care, to the human reader that's what it says.

@lpil
Copy link
Contributor Author

lpil commented Jun 2, 2024

If I had a -type result(Data, Error) :: {ok, Data} | {error, Error}. and did result(X, X) would that do it?

@IngelaAndin IngelaAndin added the team:VM Assigned to OTP team VM label Jun 3, 2024
@jhogberg
Copy link
Contributor

jhogberg commented Jun 3, 2024

Nope. :/

@jhogberg jhogberg self-assigned this Jun 3, 2024
@lpil
Copy link
Contributor Author

lpil commented Jun 4, 2024

Ah, ok. Should I close this issue then? Seems I have misunderstood how typespecs work.

@jhogberg
Copy link
Contributor

jhogberg commented Jun 5, 2024

Yes, let's. Variables in typespecs have been quite vague on how they work, and we're going to send in an EEP with clarifications to the type language Soon™.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM
Projects
None yet
Development

No branches or pull requests

5 participants