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

QuickFix for ValueNotContainedMutabilityLiteralConstantValuesDiffer #546

Draft
wants to merge 25 commits into
base: net233
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b5f622a
Quickfix for difference in mutability in signature file.
nojaf Jul 5, 2023
676ef15
Update error message in the ID.
nojaf Jul 5, 2023
3e7d62e
Start working on quickfix for ValueNotContainedMutabilityLiteralConst…
dawedawe Jul 6, 2023
b94f311
rename
dawedawe Jul 7, 2023
243dc20
refactor
dawedawe Jul 7, 2023
9c36e72
update type in signature file if needed
dawedawe Jul 7, 2023
d6b0b5e
add test case with inline comments
dawedawe Jul 7, 2023
3139293
rename error to LiteralConstantValuesDifferError, no need to use the …
dawedawe Jul 10, 2023
1cd7b7c
format changes
dawedawe Jul 10, 2023
215b53a
check if type of impl can be resolved in sig before making fix available
dawedawe Jul 10, 2023
38dd977
- handle Literals with other Literals as their expression
dawedawe Jul 10, 2023
c2461c2
add test for complex expressions fom lang preview
dawedawe Jul 10, 2023
12c4470
improve naming of tests
dawedawe Jul 11, 2023
eb494d3
add overload of ResolveWithFcs that takes a context node
dawedawe Jul 12, 2023
cf75f65
add test with parens in the pattern
dawedawe Jul 12, 2023
3b96855
- improve naming
dawedawe Jul 12, 2023
119aca3
Add validation of binary expressions
dawedawe Jul 12, 2023
e8d43c8
the FCS error can also mean that the impl side lacks the Literal attr…
dawedawe Jul 13, 2023
4cf1127
- use IgnoreInnerParens()
dawedawe Jul 13, 2023
fc3f30a
tighten match of expression
dawedawe Jul 14, 2023
9b12f36
rename "UpdateLiteralConstantFix" to "UpdateLiteralConstantInSignatur…
dawedawe Jul 14, 2023
71d8675
rename error from "LiteralConstantValuesDiffer" to "LiteralConstantVa…
dawedawe Jul 14, 2023
18b5e8c
for the time being, don't offer the fix for measure constants
dawedawe Jul 26, 2023
35a4825
Support validation checks for Constants with units of measure types
dawedawe Jul 27, 2023
884e2a6
Don't update the return type for UoM as FSharpType.Format is currentl…
dawedawe Aug 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ type FcsErrorsStageProcessBase(fsFile, daemonProcess) =
| ValueNotContainedMutability ->
if error.Message.EndsWith("The mutability attributes differ") then
createHighlightingFromNodeWithMessage ValueNotContainedMutabilityAttributesDifferError range error
elif error.Message.EndsWith("The literal constant values and/or attributes differ") then
createHighlightingFromNodeWithMessage LiteralConstantValuesDifferInSignatureError range error
else
createGenericHighlighting error range

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Compile Include="src\QuickFixes\RemovePatternArgumentFix.fs" />
<Compile Include="src\QuickFixes\AddSetterFix.fs" />
<Compile Include="src\QuickFixes\UpdateMutabilityInSignatureFix.fs" />
<Compile Include="src\QuickFixes\UpdateLiteralConstantInSignatureFix.fs" />
<ErrorsGen Include="..\FSharp.Psi.Services\src\Daemon\Highlightings\Errors.xml">
<Mode>QUICKFIX</Mode>
<Namespace>JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Daemon.QuickFixes</Namespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
namespace JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Daemon.QuickFixes

open FSharp.Compiler.Symbols
open JetBrains.ReSharper.Plugins.FSharp.Psi
open JetBrains.ReSharper.Plugins.FSharp.Psi.Impl
open JetBrains.ReSharper.Plugins.FSharp.Psi.Tree
open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Daemon.Highlightings
open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Util
open JetBrains.ReSharper.Psi.ExtensionsAPI
open JetBrains.ReSharper.Psi.Tree
open JetBrains.ReSharper.Resources.Shell

type UpdateLiteralConstantInSignatureFix(error: LiteralConstantValuesDifferInSignatureError) =
inherit FSharpQuickFixBase()
let errorRefPat = error.Pat.As<IReferencePat>()
let implNeedsLiteralAttr =
errorRefPat.Attributes
|> Seq.exists (fun attr ->
let referenceName = attr.ReferenceName
isNotNull referenceName && referenceName.ShortName = "Literal")
|> not

let tryFindDeclarationFromSignature () =
let containingTypeDecl = errorRefPat.GetContainingTypeDeclaration()
let decls = containingTypeDecl.DeclaredElement.GetDeclarations()
decls |> Seq.tryFind (fun d -> d.GetSourceFile().IsFSharpSignatureFile)

let tryFindSigBindingSignature sigMembers =
let p = errorRefPat.Binding.HeadPattern.As<IFSharpPattern>()
if Seq.length p.Declarations = 1 then
let implDec = Seq.head p.Declarations
let declName = implDec.DeclaredName
sigMembers
|> Seq.tryPick(fun m ->
let bindingSignature = m.As<IBindingSignature>()
match bindingSignature with
| null -> None
| _ ->
match bindingSignature.HeadPattern with
| :? IReferencePat as sigRefPat when
declName = sigRefPat.DeclaredName -> Some bindingSignature
| _ -> None
)
else
None

let mutable sigRefPat = null

let mutable isUoM = false

let rec isImplExprValidInSig (implExpression: IFSharpExpression) =

let opName = $"{nameof UpdateLiteralConstantInSignatureFix}.IsAvailable"

let rec collectUofMRefs (uOfM: IUnitOfMeasure) =
seq {
match uOfM with
| :? INamedMeasure as named ->
yield named.TypeUsage.As<INamedTypeUsage>().ReferenceName.Reference
| :? IProductMeasure as product ->
yield! collectUofMRefs product.Measure1
yield! collectUofMRefs product.Measure2
| :? ISeqMeasure as seqM ->
for m in seqM.Measures do
yield! collectUofMRefs m
| :? IDivideMeasure as divide ->
yield! collectUofMRefs divide.Measure1
yield! collectUofMRefs divide.Measure2
| :? IPowerMeasure as power ->
yield! collectUofMRefs power.Measure
| :? IParenMeasure as paren ->
yield! collectUofMRefs paren.Measure
| _ -> ()
}

let isValidUofMInSig (uOfM: IUnitOfMeasureClause) =
if isNull uOfM.Measure then false else

let refs = collectUofMRefs uOfM.Measure
refs |> Seq.forall (
fun r -> r.ResolveWithFcs(
sigRefPat, opName, false, true)
|> Option.isSome)


match implExpression.IgnoreInnerParens() with
| :? IReferenceExpr as refExpr ->
refExpr.Reference.ResolveWithFcs(
sigRefPat, opName, true, true)
|> Option.isSome
| :? IBinaryAppExpr as binExpr ->
isImplExprValidInSig binExpr.LeftArgument && isImplExprValidInSig binExpr.RightArgument
| :? ILiteralExpr as litExpr ->
if isNull litExpr.UnitOfMeasure then true else
isUoM <- true
isValidUofMInSig litExpr.UnitOfMeasure
| _ -> false

override x.Text =
if implNeedsLiteralAttr then $"Add Literal attribute to constant {errorRefPat.Identifier.Name}"
else $"Update literal constant {errorRefPat.Identifier.Name} in signature"

override x.IsAvailable _ =
if isNull errorRefPat then false else

match tryFindDeclarationFromSignature () with
| Some sigDecl ->
let sigMembers = sigDecl.As<IModuleDeclaration>().Members
let sigBindingSignature = tryFindSigBindingSignature sigMembers
match sigBindingSignature with
| None -> false
| Some s ->
match s.HeadPattern with
| :? IReferencePat as sRefPat ->
sigRefPat <- sRefPat
isImplExprValidInSig errorRefPat.Binding.Expression
| _ -> false
| _ -> false

override x.ExecutePsiTransaction _ =
use writeCookie = WriteLockCookie.Create(sigRefPat.IsPhysical())
use disableFormatter = new DisableCodeFormatter()

let sigSymbolUse = sigRefPat.GetFcsSymbolUse()
let implSymbolUse = errorRefPat.GetFcsSymbolUse()
let implMfv = implSymbolUse.Symbol :?> FSharpMemberOrFunctionOrValue
let sigMfv = sigSymbolUse.Symbol :?> FSharpMemberOrFunctionOrValue
// currenty FSharpType.Format is broken for UoM https://github.com/dotnet/fsharp/issues/15843
if implMfv.FullType.BasicQualifiedName <> sigMfv.FullType.BasicQualifiedName && not isUoM then
let returnTypeString = implMfv.ReturnParameter.Type.Format(sigSymbolUse.DisplayContext)
let factory = sigRefPat.CreateElementFactory()
let typeUsage = factory.CreateTypeUsage(returnTypeString, TypeUsageContext.TopLevel)
sigRefPat.Binding.ReturnTypeInfo.SetReturnType(typeUsage) |> ignore

sigRefPat.Binding.SetExpression(errorRefPat.Binding.Expression.Copy()) |> ignore

// the FCS error can also mean that the impl side lacks the literal attribute
if implNeedsLiteralAttr then
FSharpAttributesUtil.addAttributeListToLetBinding true (errorRefPat.Binding.As<IBinding>())
let attrList = errorRefPat.Binding.AttributeLists[0]
let attribute = errorRefPat.CreateElementFactory().CreateAttribute("Literal")
FSharpAttributesUtil.addAttribute attrList attribute |> ignore
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@
<QuickFix>UpdateMutabilityInSignatureFix</QuickFix>
</Error>

<Error staticGroup="FSharpErrors" name="LiteralConstantValuesDifferInSignature" ID="FS0034: Module contains but its signature specifies The literal constant values and/or attributes differ">
<Parameter type="ITopReferencePat" name="pat"/>
<Parameter type="string" name="fcsMessage"/>
<Range>pat.GetNavigationRange()</Range>
<Message resourceName="Message" resourceType="Strings">
<Argument>fcsMessage</Argument>
</Message>
<Behavour overlapResolvePolicy="NONE"/>
<QuickFix>UpdateLiteralConstantInSignatureFix</QuickFix>
</Error>

<Error staticGroup="FSharpErrors" name="VarBoundTwice" ID="FS0038: Pattern bound twice">
<Parameter type="IReferencePat" name="pat"/>
<Message resourceName="IsBoundMultipleTimesMessage" resourceType="Strings">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ public void SetQualifier([NotNull] IClrDeclaredElement declaredElement)

/// Does not reuse existing file resolve results, does complete lookup by name.
public FSharpOption<FSharpSymbolUse> ResolveWithFcs([NotNull] string opName, bool resolveExpr, bool qualified)
{
var context = GetElement().FSharpIdentifier;

return ResolveWithFcs(context, opName, resolveExpr, qualified);
}

/// Does not reuse existing file resolve results, does complete lookup by name.
public FSharpOption<FSharpSymbolUse> ResolveWithFcs([NotNull] IFSharpTreeNode context, [NotNull] string opName,
bool resolveExpr, bool qualified)
{
var referenceOwner = GetElement();
var checkerService = referenceOwner.CheckerService;
Expand All @@ -136,7 +145,7 @@ public FSharpOption<FSharpSymbolUse> ResolveWithFcs([NotNull] string opName, boo
? qualifiableReferenceOwner.Names
: new[] {GetName()};

return checkerService.ResolveNameAtLocation(referenceOwner.FSharpIdentifier, names, resolveExpr, opName);
return checkerService.ResolveNameAtLocation(context, names, resolveExpr, opName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m * kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m * kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42< / m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42< / m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23< / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23< / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<(m * kg)>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<(m * kg)>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module A

[<Measure>] type m
[<Measure>] type kg
[<Measure>] type s

[<Literal>]
let a{caret} = 42<m kg s>
Loading