Skip to content

Align DU case augmentation with previous behavior in EraseUnions #16571

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

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/8.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Changed

* Autogenerated .Is* members for unions skipped for single-case unions. ([PR 16571](https://github.com/dotnet/fsharp/pull/16571))
* `implicitCtorSynPats` in `SynTypeDefnSimpleRepr.General` is now `SynPat option` instead of `SynSimplePats option`. ([PR #16425](https://github.com/dotnet/fsharp/pull/16425))
* `SyntaxVisitorBase<'T>.VisitSimplePats` now takes `SynPat` instead of `SynSimplePat list`. ([PR #16425](https://github.com/dotnet/fsharp/pull/16425))
* Reduce allocations in compiler checking via `ValueOption` usage ([PR #16323](https://github.com/dotnet/fsharp/pull/16323))
Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,8 @@ module AddAugmentationDeclarations =

let ShouldAugmentUnion (g: TcGlobals) (tycon: Tycon) =
g.langVersion.SupportsFeature LanguageFeature.UnionIsPropertiesVisible &&
HasDefaultAugmentationAttribute g (mkLocalTyconRef tycon)
HasDefaultAugmentationAttribute g (mkLocalTyconRef tycon) &&
tycon.UnionCasesArray.Length > 1

let AddUnionAugmentationValues (cenv: cenv) (env: TcEnv) tycon =
let tcref = mkLocalTyconRef tycon
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/SyntaxTree/ParseHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ let mkSynField

let mType, mStart =
idOpt
|> Option.map _.idRange
|> Option.map (fun x -> x.idRange)
|> Option.orElseWith (fun _ -> vis |> Option.map (fun v -> v.Range))
|> Option.orElse isMutable
|> Option.orElseWith (fun _ -> leadingKeyword |> Option.map (fun k -> k.Range))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ if foo.IsBar then failwith "Should not be Bar"
|> compileExeAndRun
|> shouldSucceed

[<FSharp.Test.FactForNETCOREAPP>]
let ``Simple Is* discriminated union properties are not visible for a single case union`` () =
Fsx """
type Foo = Bar of string
let foo = Foo.Bar "hi"
if not foo.IsBar then failwith "Should be Bar"

"""
|> withLangVersionPreview
|> typecheck
|> shouldFail
|> withDiagnostics [Error 39, Line 4, Col 12, Line 4, Col 17, "The type 'Foo' does not define the field, constructor or member 'IsBar'. Maybe you want one of the following:
Bar"]

[<FSharp.Test.FactForNETCOREAPP>]
let ``Simple Is* discriminated union property satisfies SRTP constraint`` () =
Fsx """
Expand Down
1 change: 0 additions & 1 deletion tests/service/ExprTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3442,7 +3442,6 @@ let ``Test ProjectForWitnesses2`` useTransparentCompiler =
"member Neg(p) = {x = Operators.op_UnaryNegation<Microsoft.FSharp.Core.int> (fun arg0_0 -> LanguagePrimitives.UnaryNegationDynamic<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (arg0_0),p.x); y = Operators.op_UnaryNegation<Microsoft.FSharp.Core.int> (fun arg0_0 -> LanguagePrimitives.UnaryNegationDynamic<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (arg0_0),p.y)} @ (7,34--7,56)";
"member op_Addition(p1,p2) = {x = Operators.op_Addition<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (fun arg0_0 -> fun arg1_0 -> LanguagePrimitives.AdditionDynamic<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (arg0_0,arg1_0),p1.x,p2.x); y = Operators.op_Addition<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (fun arg0_0 -> fun arg1_0 -> LanguagePrimitives.AdditionDynamic<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (arg0_0,arg1_0),p1.y,p2.y)} @ (8,33--8,68)";
"type MyNumber";
"member get_IsMyNumber(this) (unitArg) = (if this.IsMyNumber then True else False) @ (10,5--10,13)";
"member get_Zero(unitVar0) = MyNumber(0) @ (12,25--12,35)";
"member op_Addition(_arg1,_arg2) = let x: Microsoft.FSharp.Core.int = _arg1.Item in let y: Microsoft.FSharp.Core.int = _arg2.Item in MyNumber(Operators.op_Addition<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (fun arg0_0 -> fun arg1_0 -> LanguagePrimitives.AdditionDynamic<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (arg0_0,arg1_0),x,y)) @ (13,23--13,33)";
"member DivideByInt(_arg3,i) = let x: Microsoft.FSharp.Core.int = _arg3.Item in MyNumber(Operators.op_Division<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (fun arg0_0 -> fun arg1_0 -> LanguagePrimitives.DivisionDynamic<Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int,Microsoft.FSharp.Core.int> (arg0_0,arg1_0),x,i)) @ (15,31--15,41)";
Expand Down
10 changes: 4 additions & 6 deletions tests/service/ProjectAnalysisTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,11 @@ let ``Test project2 all symbols in signature`` () =
"DUWithNormalFields"; "member get_IsD"; "member get_IsDU1"; "member get_IsDU2";
"property IsD"; "property IsDU1"; "property IsDU2"; "DU1"; "field Item1";
"field Item2"; "DU2"; "field Item1"; "field Item2"; "D"; "field Item1";
"field Item2"; "DUWithNamedFields"; "member get_IsDU"; "property IsDU"; "DU";
"field Item2"; "DUWithNamedFields"; "DU";
"field x"; "field y"; "GenericClass`1"; "generic parameter T"; "member .ctor";
"member GenericMethod"; "generic parameter U"] |> List.sort

shouldEqual e r
shouldPairwiseEqual e r

[<Test>]
let ``Test project2 all uses of all signature symbols`` () =
Expand All @@ -736,12 +736,10 @@ let ``Test project2 all uses of all signature symbols`` () =
("generic parameter T",
[("file1", ((22, 23), (22, 25))); ("file1", ((22, 30), (22, 32)));
("file1", ((22, 45), (22, 47))); ("file1", ((22, 50), (22, 52)))]);
("member get_IsD", []);
("member get_IsDU", []);
("member get_IsD", []);
("member get_IsDU1", []);
("member get_IsDU2", []);
("property IsD", []);
("property IsDU", []);
("property IsD", []);
("property IsDU1", []);
("property IsDU2", []);
("DUWithNormalFields", [("file1", ((3, 5), (3, 23)))]);
Expand Down