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

Account for AllowNullLiteralAttribute when checking attribute targets #17583

Merged
merged 9 commits into from
Aug 23, 2024
10 changes: 7 additions & 3 deletions src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2855,9 +2855,13 @@ module EstablishTypeDefinitionCores =
let hasMeasureAttr = HasFSharpAttribute g g.attrib_MeasureAttribute attrs
let hasStructAttr = HasFSharpAttribute g g.attrib_StructAttribute attrs
let hasCLIMutable = HasFSharpAttribute g g.attrib_CLIMutableAttribute attrs
// CLIMutableAttribute has a special treatment(specific error FS3132) in the case of records(Only record types may have this attribute.)
// So we want to keep these special treatment for records and avoid having two errors for the same attribute.
let reportAttributeTargetsErrors = g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) && not hasCLIMutable
let hasAllowNullLiteralAttr = HasFSharpAttribute g g.attrib_AllowNullLiteralAttribute attrs

// We want to keep these special attributes treatment and avoid having two errors for the same attribute.
let reportAttributeTargetsErrors =
g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets)
&& not hasCLIMutable // CLIMutableAttribute has a special treatment(specific error FS3132)
&& not hasAllowNullLiteralAttr // AllowNullLiteralAttribute has a special treatment(specific errors FS0934, FS093)

let noCLIMutableAttributeCheck() =
if hasCLIMutable then errorR (Error(FSComp.SR.tcThisTypeMayNotHaveACLIMutableAttribute(), m))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[<AllowNullLiteral>]
type D() =
member x.P = 1
Original file line number Diff line number Diff line change
Expand Up @@ -738,4 +738,56 @@ type InterruptibleLazy<'T> private (valueFactory: unit -> 'T) =
(Error 3132, Line 19, Col 8, Line 19, Col 19, "This type definition may not have the 'CLIMutable' attribute. Only record types may have this attribute.")
(Error 3132, Line 22, Col 8, Line 22, Col 17, "This type definition may not have the 'CLIMutable' attribute. Only record types may have this attribute.")
(Error 3132, Line 25, Col 8, Line 25, Col 18, "This type definition may not have the 'CLIMutable' attribute. Only record types may have this attribute.")
]
]

// SOURCE= E_AllowNullLiteral.fs # E_AllowNullLiteral.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AllowNullLiteral.fs"|])>]
let ``E_AllowNullLiteral 8.0`` compilation =
compilation
|> withLangVersion80
|> verifyCompile
|> shouldFail
|> withDiagnostics [
(Error 935, Line 15, Col 10, Line 15, Col 11, "Types with the 'AllowNullLiteral' attribute may only inherit from or implement types which also allow the use of the null literal")
(Error 934, Line 27, Col 10, Line 27, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 30, Col 10, Line 30, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 33, Col 10, Line 33, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 36, Col 10, Line 36, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 39, Col 10, Line 39, Col 13, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 842, Line 41, Col 7, Line 41, Col 23, "This attribute is not valid for use on this language element")
(Error 842, Line 44, Col 7, Line 44, Col 23, "This attribute is not valid for use on this language element")
]

// SOURCE=E_AllowNullLiteral.fs # E_AllowNullLiteral.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AllowNullLiteral.fs"|])>]
let ``E_AllowNullLiteral preview`` compilation =
compilation
|> withLangVersionPreview
|> verifyCompile
|> shouldFail
|> withDiagnostics [
(Error 935, Line 15, Col 10, Line 15, Col 11, "Types with the 'AllowNullLiteral' attribute may only inherit from or implement types which also allow the use of the null literal")
(Error 934, Line 27, Col 10, Line 27, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 30, Col 10, Line 30, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 33, Col 10, Line 33, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 36, Col 10, Line 36, Col 11, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 934, Line 39, Col 10, Line 39, Col 13, "Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute")
(Error 842, Line 41, Col 7, Line 41, Col 23, "This attribute is not valid for use on this language element");
(Error 842, Line 44, Col 7, Line 44, Col 23, "This attribute is not valid for use on this language element")
]

// SOURCE= AllowNullLiteral01.fs # AllowNullLiteral01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AllowNullLiteral01.fs"|])>]
let ``AllowNullLiteral01 8.0`` compilation =
compilation
|> withLangVersion80
|> verifyCompile
|> shouldSucceed

// SOURCE=AllowNullLiteral01.fs # AllowNullLiteral01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AllowNullLiteral01.fs"|])>]
let ``AllowNullLiteral01 preview`` compilation =
compilation
|> withLangVersionPreview
|> verifyCompile
|> shouldSucceed
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module AllowNullLiteralTest = begin

//[<AllowNullLiteral>]
type I =
interface
abstract P : int
end

//[<AllowNullLiteral>]
type C() =
member x.P = 1


[<AllowNullLiteral>]
type D() =
inherit C()
interface I with
member x.P = 2
member x.P = 1

let d = (null : D)

let d2 = ((box null) :?> D)


[<AllowNullLiteral>] // expect an error here
type S(c:int) = struct end

[<AllowNullLiteral>] // expect an error here
type R = { r : int }

[<AllowNullLiteral>] // expect an error here
type U = A | B of int

[<AllowNullLiteral>] // expect an error here
type E = A = 1 | B = 2

[<AllowNullLiteral>] // expect an error here
type Del = delegate of int -> int

[<AllowNullLiteral>] // expect an error here
let x = 1

[<AllowNullLiteral>] // expect an error here
let f x = 1

end

6 changes: 0 additions & 6 deletions tests/fsharp/typecheck/sigs/neg16.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ neg16.fs(7,13,7,16): typecheck error FS0644: Namespaces cannot contain extension

neg16.fs(23,10,23,11): typecheck error FS0935: Types with the 'AllowNullLiteral' attribute may only inherit from or implement types which also allow the use of the null literal

neg16.fs(34,7,34,23): typecheck error FS0842: This attribute is not valid for use on this language element

neg16.fs(35,10,35,11): typecheck error FS0934: Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute

neg16.fs(38,10,38,11): typecheck error FS0934: Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute

neg16.fs(41,10,41,11): typecheck error FS0934: Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute

neg16.fs(43,7,43,23): typecheck error FS0842: This attribute is not valid for use on this language element

neg16.fs(44,10,44,11): typecheck error FS0934: Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute

neg16.fs(46,7,46,23): typecheck error FS0842: This attribute is not valid for use on this language element

neg16.fs(47,10,47,13): typecheck error FS0934: Records, union, abbreviations and struct types cannot have the 'AllowNullLiteral' attribute

neg16.fs(49,7,49,23): typecheck error FS0842: This attribute is not valid for use on this language element
Expand Down
Loading