Skip to content

Commit

Permalink
Merge pull request fsprojects#676 from ovatsus/NonNullable
Browse files Browse the repository at this point in the history
Add AllowNullLiteralAttribute(false) to erased types with constructors
  • Loading branch information
Gustavo Guerra committed Sep 10, 2014
2 parents 3f351af + 587f57d commit 7e0ea40
Show file tree
Hide file tree
Showing 58 changed files with 505 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/CommonProviderImplementation/Debug.fs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ module Debug =
|> Seq.sortBy (fun m -> m.Name)
|> Seq.truncate maxWidth
for t in pendingForThisDepth do
for attr in t.GetCustomAttributesData() do
print <| (sprintf "[<%A>]" attr).Replace("Microsoft.FSharp.Core.", null).Replace("CompilerServices.", null).Replace("Attribute(", "(")
println()
match t with
| t when FSharpType.IsRecord t-> "record "
| t when FSharpType.IsModule t -> "module "
Expand Down
14 changes: 13 additions & 1 deletion src/CommonProviderImplementation/ProvidedTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ module internal Misc =
member __.ConstructorArguments = upcast [| |]
member __.NamedArguments = upcast [| |] }

let mkAllowNullLiteralCustomAttributeData value =
#if FX_NO_CUSTOMATTRIBUTEDATA
{ new IProvidedCustomAttributeData with
#else
{ new CustomAttributeData() with
#endif
member __.Constructor = typeof<AllowNullLiteralAttribute>.GetConstructors().[0]
member __.ConstructorArguments = upcast [| CustomAttributeTypedArgument(typeof<bool>, value) |]
member __.NamedArguments = upcast [| |] }
/// This makes an xml doc attribute w.r.t. an amortized computation of an xml doc string.
/// It is important that the text of the xml doc only get forced when poking on the ConstructorArguments
/// for the CustomAttributeData object.
Expand Down Expand Up @@ -122,6 +131,7 @@ module internal Misc =
type CustomAttributesImpl() =
let customAttributes = ResizeArray<CustomAttributeData>()
let mutable hideObjectMethods = false
let mutable nonNullable = false
let mutable obsoleteMessage = None
let mutable xmlDocDelayed = None
let mutable xmlDocAlwaysRecomputed = None
Expand All @@ -137,6 +147,7 @@ module internal Misc =
let customAttributesOnce =
lazy
[| if hideObjectMethods then yield mkEditorHideMethodsCustomAttributeData()
if nonNullable then yield mkAllowNullLiteralCustomAttributeData false
match xmlDocDelayed with None -> () | Some _ -> customAttributes.Add(mkXmlDocCustomAttributeDataLazy xmlDocDelayedText)
match obsoleteMessage with None -> () | Some s -> customAttributes.Add(mkObsoleteAttributeCustomAttributeData s)
if hasParamArray then yield mkParamArrayCustomAttributeData()
Expand All @@ -149,6 +160,7 @@ module internal Misc =
member __.AddXmlDocDelayed(xmlDoc : unit -> string) = xmlDocDelayed <- Some xmlDoc
member this.AddXmlDoc(text:string) = this.AddXmlDocDelayed (fun () -> text)
member __.HideObjectMethods with set v = hideObjectMethods <- v
member __.NonNullable with set v = nonNullable <- v
member __.AddCustomAttribute(attribute) = customAttributes.Add(attribute)
member __.GetCustomAttributesData() =
[| yield! customAttributesOnce.Force()
Expand Down Expand Up @@ -363,7 +375,6 @@ type ProvidedConstructor(parameters : ProvidedParameter list) =
member this.AddXmlDoc xmlDoc = customAttributesImpl.AddXmlDoc xmlDoc
member this.AddObsoleteAttribute (msg,?isError) = customAttributesImpl.AddObsolete (msg,defaultArg isError false)
member this.AddDefinitionLocation(line,column,filePath) = customAttributesImpl.AddDefinitionLocation(line, column, filePath)
member this.HideObjectMethods with set v = customAttributesImpl.HideObjectMethods <- v
member __.GetCustomAttributesDataImpl() = customAttributesImpl.GetCustomAttributesData()
#if FX_NO_CUSTOMATTRIBUTEDATA
#else
Expand Down Expand Up @@ -1095,6 +1106,7 @@ type ProvidedTypeDefinition(container:TypeContainer,className : string, baseType
member this.AddObsoleteAttribute (msg,?isError) = customAttributesImpl.AddObsolete (msg,defaultArg isError false)
member this.AddDefinitionLocation(line,column,filePath) = customAttributesImpl.AddDefinitionLocation(line, column, filePath)
member this.HideObjectMethods with set v = customAttributesImpl.HideObjectMethods <- v
member this.NonNullable with set v = customAttributesImpl.NonNullable <- v
member __.GetCustomAttributesDataImpl() = customAttributesImpl.GetCustomAttributesData()
member this.AddCustomAttribute attribute = customAttributesImpl.AddCustomAttribute attribute
#if FX_NO_CUSTOMATTRIBUTEDATA
Expand Down
3 changes: 3 additions & 0 deletions src/CommonProviderImplementation/ProvidedTypes.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ type ProvidedTypeDefinition =
/// Suppress System.Object entries in intellisense menus in instances of this provided type
member HideObjectMethods : bool with set

/// Disallows the use of the null literal.
member NonNullable : bool with set

/// Get or set a flag indicating if the ProvidedTypeDefinition is erased
member IsErased : bool with get,set

Expand Down
5 changes: 3 additions & 2 deletions src/Json/JsonGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ module JsonTypeBuilder =
|> ctx.UniqueNiceName

// Generate new type for the heterogeneous type
let objectTy = ProvidedTypeDefinition(typeName, Some(ctx.IJsonDocumentType), HideObjectMethods = true)
let objectTy = ProvidedTypeDefinition(typeName, Some(ctx.IJsonDocumentType), HideObjectMethods = true, NonNullable = true)
ctx.TypeProviderType.AddMember objectTy

// to nameclash property names
Expand Down Expand Up @@ -280,7 +280,8 @@ module JsonTypeBuilder =
|> ctx.UniqueNiceName

// Generate new type for the record
let objectTy = ProvidedTypeDefinition(name, Some(ctx.IJsonDocumentType), HideObjectMethods = true)
let objectTy = ProvidedTypeDefinition(name, Some(ctx.IJsonDocumentType), HideObjectMethods = true, NonNullable = true)

ctx.TypeProviderType.AddMember(objectTy)

// to nameclash property names
Expand Down
6 changes: 3 additions & 3 deletions src/Xml/XmlGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ module internal XmlTypeBuilder =
| HeterogeneousRecords cases ->

// Generate new choice type for the element
let objectTy = ProvidedTypeDefinition(ctx.UniqueNiceName "Choice", Some(ctx.Replacer.ToRuntime typeof<XmlElement>), HideObjectMethods = true)
let objectTy = ProvidedTypeDefinition(ctx.UniqueNiceName "Choice", Some(ctx.Replacer.ToRuntime typeof<XmlElement>), HideObjectMethods = true, NonNullable = true)
ctx.TypeProviderType.AddMember objectTy

// to nameclash property names
Expand Down Expand Up @@ -239,7 +239,7 @@ module internal XmlTypeBuilder =

let objectTy = ProvidedTypeDefinition(ctx.UniqueNiceName names.[0],
Some(ctx.Replacer.ToRuntime typeof<XmlElement>),
HideObjectMethods = true)
HideObjectMethods = true, NonNullable = true)
ctx.TypeProviderType.AddMember objectTy

// If we unify types globally, then save type for this record
Expand Down Expand Up @@ -280,7 +280,7 @@ module internal XmlTypeBuilder =
// If the attribute has multiple possible type (e.g. "bool|int") then we generate
// a choice type that is erased to 'option<string>' (for simplicity, assuming that
// the attribute is always optional)
let choiceTy = ProvidedTypeDefinition(ctx.UniqueNiceName (name + "Choice"), Some(ctx.Replacer.ToRuntime typeof<option<string>>), HideObjectMethods = true)
let choiceTy = ProvidedTypeDefinition(ctx.UniqueNiceName (name + "Choice"), Some(ctx.Replacer.ToRuntime typeof<option<string>>), HideObjectMethods = true, NonNullable = true)
ctx.TypeProviderType.AddMember choiceTy

for KeyValue(tag, typ) in types do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -188,6 +189,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ";", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : float * float * decimal * int * int * int
member Day: int with get
(let _,_,_,_,_,t6 = this in t6)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -308,6 +309,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : System.DateTime * string * string * string * string * string * string * string * string * string * string
member AUD: string with get
(let _,_,_,_,_,_,_,_,_,_,t11 = this in t11)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -180,6 +181,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), " ", '"', false, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : string * System.DateTime * System.Guid option * string * string * string
member Column1: string with get
(let t1,_,_,_,_,_ = this in t1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -212,6 +213,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : System.DateTime * decimal * decimal * decimal * decimal * int * decimal
member Adj Close: decimal with get
(let _,_,_,_,_,_,t7 = this in t7)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -116,6 +117,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : string * decimal * decimal
member Distance: decimal<metre> with get
(let _,t2,_ = this in t2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -332,6 +333,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : int * bool * int * string * string * float * int * int * string * decimal * string * string
member Age: float with get
(let _,_,_,_,_,t6,_,_,_,_,_,_ = this in t6)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -252,6 +253,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : int * bool option * int option * string option * string option * decimal option * int option * int * string option * decimal option * string option * string option
member Age: decimal option with get
(let _,_,_,_,_,t6,_,_,_,_,_,_ = this in t6)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -92,6 +93,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : int * float
member id: int with get
(let t1,_ = this in t1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -92,6 +93,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : int * decimal
member Distance: int<metre> with get
(let t1,_ = this in t1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[<[TypeProviderEditorHideMethods()]>]
class CsvProvider : FDR.CsvFile<CsvProvider+Row>
new : () -> CsvProvider
let stringArrayToRow = new Func<_,_,_>(fun (parent:obj) (row:string[]) ->
Expand Down Expand Up @@ -332,6 +333,7 @@ class CsvProvider : FDR.CsvFile<CsvProvider+Row>
CsvFile<_>.Create(stringArrayToRow, rowToStringArray, ((new StringReader(text)) :> TextReader), ",", '"', true, false, false)


[<[TypeProviderEditorHideMethods()]>]
class CsvProvider+Row : int * string * string * float * float * float * float * float * string * string * string * System.DateTime
member Bat1: string with get
(let _,t2,_,_,_,_,_,_,_,_,_,_ = this in t2)
Expand Down
Loading

0 comments on commit 7e0ea40

Please sign in to comment.