From 4a26619f6b3d664983816615121286f6e837f6bd Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Tue, 13 Aug 2024 16:36:10 +0100 Subject: [PATCH] More tests --- src/Compiler/Facilities/LanguageFeatures.fs | 1 + .../ObjectExpressions/ObjectExpressions.fs | 68 ++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Compiler/Facilities/LanguageFeatures.fs b/src/Compiler/Facilities/LanguageFeatures.fs index 9ab3292ff3e..ccf2796ca90 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fs +++ b/src/Compiler/Facilities/LanguageFeatures.fs @@ -218,6 +218,7 @@ type LanguageVersion(versionText) = LanguageFeature.EnforceAttributeTargets, previewVersion // not enabled because: https://github.com/dotnet/fsharp/issues/17514 LanguageFeature.FromEndSlicing, previewVersion // Unfinished features --- needs work LanguageFeature.AllowAccessModifiersToAutoPropertiesGettersAndSetters, previewVersion + LanguageFeature.AllowObjectExpressionWithoutOverrides, previewVersion ] static let defaultLanguageVersion = LanguageVersion("default") diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs index 3e60ffb6df7..d9db8772bf9 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs @@ -235,6 +235,72 @@ let foo2 = { new Foo() with member __.ToString() = base.ToString() } |> withDiagnostics [ (Error 738, Line 5, Col 11, Line 5, Col 24, "Invalid object expression. Objects without overrides or interfaces should use the expression form 'new Type(args)' without braces."); (Error 759, Line 7, Col 12, Line 7, Col 21, "Instances of this type cannot be created since it has been marked abstract or not all methods have been given implementations. Consider using an object expression '{ new ... with ... }' instead.") + ] + + [] + let ``Object expression can not implement an abstract class having no abstract members`` () = + Fsx """ +[] +type AbstractClass() = + do printfn "AbstractClass constructor" + +let res = { new AbstractClass() } + """ + |> withLangVersion80 + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 738, Line 6, Col 11, Line 6, Col 34, "Invalid object expression. Objects without overrides or interfaces should use the expression form 'new Type(args)' without braces.") + ] + + [] + let ``Object expression can not implement an abstract class having abstract members`` () = + Fsx """ +[] +type AbstractClass() = + do printfn "AbstractClass constructor" + abstract member M : unit -> unit + +let res = { new AbstractClass() } + """ + |> withLangVersion80 + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 365, Line 7, Col 11, Line 7, Col 34, "No implementation was given for 'abstract AbstractClass.M: unit -> unit'"); + (Error 738, Line 7, Col 11, Line 7, Col 34, "Invalid object expression. Objects without overrides or interfaces should use the expression form 'new Type(args)' without braces.") + ] + + [] + let ``Object expression can implement an abstract class having no abstract members preview`` () = + Fsx """ +[] +type AbstractClass() = + do printfn "AbstractClass constructor" + +let res = { new AbstractClass() } + """ + |> withLangVersionPreview + |> compileExeAndRun + |> withStdOutContainsAllInOrder [ + "AbstractClass constructor" + ] + + [] + let ``Object expression can not implement an abstract class having abstract members preview.`` () = + Fsx """ +[] +type AbstractClass() = + do printfn "AbstractClass constructor" + abstract member M : unit -> unit + +let res = { new AbstractClass() } + """ + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 365, Line 7, Col 11, Line 7, Col 34, "No implementation was given for 'abstract AbstractClass.M: unit -> unit'") ] [] @@ -384,7 +450,7 @@ let res = { new Animal() } |> compile |> shouldFail |> withDiagnostics [ - // Error missing + // FIXME ] []