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

Disallow attributes on type extensions #7481

Merged
merged 5 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 src/fsharp/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,7 @@ notAFunctionButMaybeDeclaration,"This value is not a function and cannot be appl
3243,parsInvalidAnonRecdExpr,"Invalid anonymous record expression"
3244,parsInvalidAnonRecdType,"Invalid anonymous record type"
3245,tcCopyAndUpdateNeedsRecordType,"The input to a copy-and-update expression that creates an anonymous record must be either an anonymous record or a record"
3246,tcAugmentationsCannotHaveAttributes,"Attributes cannot be applied to type extensions."
3300,chkInvalidFunctionParameterType,"The parameter '%s' has an invalid type '%s'. This is not permitted by the rules of Common IL."
3301,chkInvalidFunctionReturnType,"The function or method has an invalid return type '%s'. This is not permitted by the rules of Common IL."
useSdkRefs,"Use reference assemblies for .NET framework references when available (Enabled by default)."
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/TypeChecker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16429,6 +16429,11 @@ module TcDeclarations =
if not (isNil members) && tcref.IsTypeAbbrev then
errorR(Error(FSComp.SR.tcTypeAbbreviationsCannotHaveAugmentations(), tyDeclRange))

let (ComponentInfo (attributes, _, _, _, _, _, _, _)) = synTyconInfo
if not (List.isEmpty attributes) && (declKind = ExtrinsicExtensionBinding || declKind = IntrinsicExtensionBinding) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

let attributeRange = (List.head attributes).Range
error(Error(FSComp.SR.tcAugmentationsCannotHaveAttributes(), attributeRange))

MutRecDefnsPhase2DataForTycon(tyconOpt, innerParent, declKind, tcref, baseValOpt, safeInitInfo, declaredTyconTypars, members, tyDeclRange, newslotsOK, fixupFinalAttrs))

// By now we've established the full contents of type definitions apart from their
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">Není definovaný obor názvů {0}.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">Der Namespace "{0}" ist nicht definiert.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">El espacio de nombres "{0}" no está definido.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">L'espace de noms '{0}' n'est pas défini.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">Lo spazio dei nomi '{0}' non è definito.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">名前空間 '{0}' が定義されていません。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">'{0}' 네임스페이스가 정의되지 않았습니다.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">Nie zdefiniowano przestrzeni nazw „{0}”.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">O namespace '{0}' não está definido.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">Пространство имен "{0}" не определено.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">'{0}' ad alanı tanımlı değil.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">未定义命名空间“{0}”。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/xlf/FSComp.txt.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target state="new">Algorithm '{0}' is not supported</target>
<note />
</trans-unit>
<trans-unit id="tcAugmentationsCannotHaveAttributes">
<source>Attributes cannot be applied to type extensions.</source>
<target state="new">Attributes cannot be applied to type extensions.</target>
<note />
</trans-unit>
<trans-unit id="undefinedNameNamespace">
<source>The namespace '{0}' is not defined.</source>
<target state="translated">未定義命名空間 '{0}'。</target>
Expand Down
44 changes: 44 additions & 0 deletions tests/fsharp/Compiler/Language/TypeAttributeTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Compiler.SourceCodeServices

[<TestFixture>]
module TypeAttributeTests =

[<Test>]
let ``Attribute can be assigned to type definition``() =
CompilerAssert.Pass
"""
[<Struct>]
type Point = {x:int; y:int}
"""
[<Test>]
let ``Attribute cannot be assigned to optional type extension``() =
CompilerAssert.TypeCheckSingleError
"""
open System

[<NoEquality>]
type String with
member this.test = 42
"""
FSharpErrorSeverity.Error
3245
(4, 1, 4, 15)
"Attributes cannot be assigned to type extensions."
nelson-wu marked this conversation as resolved.
Show resolved Hide resolved

[<Test>]
let ``Attribute cannot be assigned to intrinsic type extension``() =
CompilerAssert.TypeCheckSingleError
"""
type Point = {x:int; y:int}

[<NoEquality>]
type Point with
member this.test = 42
"""
FSharpErrorSeverity.Error
3245
(4, 1, 4, 15)
"Attributes cannot be assigned to type extensions."
1 change: 1 addition & 0 deletions tests/fsharp/FSharpSuite.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="Compiler\Warnings\AssignmentWarningTests.fs" />
<Compile Include="Compiler\Warnings\PatternMatchingWarningTests.fs" />
<Compile Include="Compiler\SourceTextTests.fs" />
<Compile Include="Compiler\Language\TypeAttributeTests.fs" />
<Compile Include="Compiler\Language\AnonRecordTests.fs" />
<Compile Include="Compiler\Language\SpanOptimizationTests.fs" />
<Compile Include="Compiler\Language\SpanTests.fs" />
Expand Down