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

Fix for #83 (improve constraint error message) #16304

Merged
merged 22 commits into from
Dec 20, 2023
Merged

Fix for #83 (improve constraint error message) #16304

merged 22 commits into from
Dec 20, 2023

Conversation

Martin521
Copy link
Contributor

@Martin521 Martin521 commented Nov 19, 2023

This fix for #83 includes

  • An addition of field "source" to TraitConstraintInfo / TTrait in TypedTree.fs/fsi
  • Code in CompilerImports.fs which, after import, adds the source of the constraint to all constraints
  • A new error message in FSComp.txt
  • Application of the error message, including the constraint source, in ConstraintSolver.fs
  • 4 new tests in MemberConstraints.fs
  • A few updated test expectations in AbsTests.fs, neg61.bsl and E_NoMethodsOnEnums01.fs
  • 29 adaptations in 9 files because of the new field in TTrait

This includes
- An addition of field "source" to TraitConstraintInfo / TTrait in TypedTree.fs/fsi
- Code in CompilerImports.fs which, after import, adds the sources to all constraints
- New error messages, including the constraint source, in ConstraintSolver.fs
- The error messages in FSComp.txt
- A new test in MemberConstraints.fs
- 29 adaptations b.o. the new field in TTrait in 9 files
@Martin521 Martin521 requested a review from a team as a code owner November 19, 2023 14:58
@Martin521
Copy link
Contributor Author

/run xlf

  Co-authored-by: Martin521 <29605222+Martin521@users.noreply.github.com>
Copy link
Contributor

@Martin521 Martin521 marked this pull request as draft November 19, 2023 19:07
@psfinaki
Copy link
Member

Hey @Martin521 - thanks for taking this long-standing issue! :)

I know there is a discussion in the related ticket - my opinion is that whatever we do will be better than the current situation. That said, for me even having DivideByInt notion in the error message is weird, feels like a leaking technical detail. The code in the new test can very well be written by some F# newcomer and that message would just explode their mind. I would prefer something like "Hey, the average function expects int result which cannot be guaranteed by division".

I see that the message in FSComp is generic for types and operators so for now I would appreciate just adding a few more test cases that are expected to produce the same error, so that we can see a bigger picture.

@Martin521
Copy link
Contributor Author

@psfinaki - thanks for your feedback!

I see that the message in FSComp is generic for types and operators so for now I would appreciate just adding a few more test cases that are expected to produce the same error, so that we can see a bigger picture.

I will do that.

I will also respond (tomorrow) to your other remarks, but in the ticket.

@smoothdeveloper
Copy link
Contributor

@psfinaki,

the average function expects int result which cannot be guaranteed by division

AFAIU, this would require infrastructure for API authors, and adjusting FSharp.Core to use it; the infrastructure would allow to give customized error message on members with constraints, given predicate on the passed / infered type arguments, to deliver a custom message.

IMO, this is out of scope in just trying to improve the constraint not matching error, which should, unless the infrastructure & special condition that it would detect in API, always be displayed, in order to give the full picture.

@vzarytovskii
Copy link
Member

That said, for me even having DivideByInt notion in the error message is weird, feels like a leaking technical detail. The code in the new test can very well be written by some F# newcomer and that message would just explode their mind. I would prefer something like "Hey, the average function expects int result which cannot be guaranteed by division".

I personally disagree with that. It doesn't have to be either/or. It should be both - a simple message as well as bunch of details.

We don't need to simplify messages in sake of simplifying them. But rather extend them.

@DedSec256 and @auduchinok may have some opinions here.

@psfinaki
Copy link
Member

No yeah for sure we shouldn't overhaul the compiler just in order to make this usecase clear.

We don't need to simplify messages in sake of simplifying them. But rather extend them.

... is definitely a valid approach.

@Martin521
Copy link
Contributor Author

Thanks @psfinaki for asking for more examples and the broader picture. This made me realize that the picture is broader indeed. I will continue with implementation (including test cases) once the discussion on the ticket is converging.

@Martin521 Martin521 marked this pull request as ready for review December 11, 2023 21:04
@Martin521
Copy link
Contributor Author

This is ready.
This fixes #83 for the limited scope mentioned here.

Also note that the extended error message only appears when the constraint source (the operator or method or function that has the constraint typar) is imported. Doing the same while that source is in your own project is more difficult, but also deemed to be not needed, because both the language guide and the spec say that creating member constraints is "not intended for common use".

@Martin521
Copy link
Contributor Author

/azp run

Copy link

Commenter does not have sufficient privileges for PR 16304 in repo dotnet/fsharp

@T-Gro
Copy link
Member

T-Gro commented Dec 19, 2023

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@vzarytovskii vzarytovskii merged commit 73970cb into dotnet:main Dec 20, 2023
26 checks passed
@vzarytovskii
Copy link
Member

Thanks, let's see how it looks in IDEs, and get feedback from people.

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.

5 participants