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

Improve value restriction error message #1103 #15877

Merged
merged 20 commits into from
Dec 18, 2023
Merged

Conversation

Happypig375
Copy link
Member

@Happypig375 Happypig375 commented Aug 27, 2023

Improve value restriction error message, relates to #1103, fixes #1161

Implemented message for value restriction error

For function values

Value restriction: The value '{0}' has an inferred generic function type
    {1} 
However, values cannot have generic type variables like '_a in "let f: '_a". You should define '{2}' as a function instead by doing one of the following:
- Add an explicit parameter that is applied instead of using a partial application like "let f param"
- Add a unit parameter like "let f()"
- Write explicit type parameters like "let f<'a>",
or if you do not intend for it to be generic, either:
- Add an explicit type annotation like "let f : obj -> obj"
- Apply arguments of non-generic types to the function value in later code for type inference like "do f()".
This error is because a let binding without parameters defines a value, not a function. Values cannot be generic because reading a value is assumed to result in the same everywhere but generic type parameters may invalidate this assumption by enabling type-dependent results.

For non-function values

Value restriction: The value '{0}' has an inferred generic type
    {1}
However, values cannot have generic type variables like '_a in "let x: '_a". You can do one of the following:
- Define it as a simple data term like an integer literal, a string literal or a union case like "let x = 1"
- Add an explicit type annotation like "let x : int"
- Use the value as a non-generic type in later code for type inference like "do x"
or if you still want type-dependent results, you can define '{2}' as a function instead by doing either:
- Add a unit parameter like "let x()"
- Write explicit type parameters like "let x<'a>".
This error is because a let binding without parameters defines a value, not a function. Values cannot be generic because reading a value is assumed to result in the same everywhere but generic type parameters may invalidate this assumption by enabling type-dependent results.

@Happypig375 Happypig375 requested a review from a team as a code owner August 27, 2023 18:37
@Happypig375 Happypig375 marked this pull request as draft August 27, 2023 18:43
@Happypig375 Happypig375 marked this pull request as ready for review September 1, 2023 08:36
@Happypig375
Copy link
Member Author

Ready for review

Copy link
Member

@vzarytovskii vzarytovskii left a comment

Choose a reason for hiding this comment

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

This lgtm, @dotnet/fsharp-team-msft

Copy link
Member

@psfinaki psfinaki left a comment

Choose a reason for hiding this comment

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

Thank you.

@vzarytovskii vzarytovskii enabled auto-merge (squash) September 18, 2023 15:51
@psfinaki
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@psfinaki
Copy link
Member

@Happypig375 looks like this somehow intertwined with the latest parens removal code fix. Would you like to look at it? I can jump on that in the upcoming days if you want to want to bother. Should be something simple though hopefully.

@psfinaki
Copy link
Member

Sorry false alarm, updating the branch.

@edgarfgp
Copy link
Contributor

Please do not let this PR stale. the new error message is much better.

@psfinaki
Copy link
Member

Thanks for reminding, Edgar.

@Happypig375 will you have a moment to resolve the conflicts and address the remaining notes? If not, I can help with that this week.

@Happypig375
Copy link
Member Author

I can revisit this next week but @psfinaki if you want you can go ahead with this PR

@psfinaki
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@psfinaki
Copy link
Member

@Happypig375 so I'll be merging this - thanks for the PR!

@psfinaki psfinaki merged commit 311360d into dotnet:main Dec 18, 2023
26 checks passed
OwnageIsMagic added a commit to OwnageIsMagic/fsharp that referenced this pull request Dec 21, 2023
* upstream/main: (166 commits)
  typo in foldBack summary (dotnet#16453)
  Fix for dotnet#83 (improve constraint error message) (dotnet#16304)
  Name resolution: resolve interfaces in expressions (dotnet#15660)
  AddExplicitReturnType refactoring (dotnet#16077)
  Disabling 2 tests: running for too long, causing CI timeouts
  Improve value restriction error message dotnet#1103 (dotnet#15877)
  Parens: Keep parens for non-identical infix operator pairs with same precedence (dotnet#16372)
  More release note entries (dotnet#16438)
  Using Ordinal is both faster and more correct as our intent is to do … (dotnet#16439)
  merge (dotnet#16427)
  Optimize empty string compares (dotnet#16435)
  Checker: recover on unresolved type in 'inherit' member (dotnet#16429)
  Release notes proposal (dotnet#16377)
  [main] Update dependencies from dotnet/source-build-reference-packages (dotnet#16411)
  Allow usage of [<TailCall>] with older FSharp.Core package versions (dotnet#16373)
  Parser: recover on unfinished 'as' patterns (dotnet#16404)
  Parens: Keep parens in method calls in dot-lambdas (dotnet#16395)
  Checker: check unfinished obj expression inside computations (dotnet#16413)
  Added default dotnet-tools + additional tasks to launch them (dotnet#16409)
  make `remarks` and `returns` visible in quick info (dotnet#16417)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Improve error reporting: Partially applied generic value vs function
4 participants