File: longnames.fs

package info (click to toggle)
fsharp 4.0.0.4%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,824 kB
  • ctags: 1,395
  • sloc: cs: 2,983; ml: 1,098; makefile: 410; sh: 409; xml: 113
file content (439 lines) | stat: -rw-r--r-- 12,582 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// #Conformance #ObjectConstructors 
#if ALL_IN_ONE
module Core_longnames
#endif
let failures = ref []

let report_failure (s : string) = 
    stderr.Write" NO: "
    stderr.WriteLine s
    failures := !failures @ [s]

let test (s : string) b = 
    stderr.Write(s)
    if b then stderr.WriteLine " OK"
    else report_failure (s)

let check s b1 b2 = test s (b1 = b2)

#if NetCore
#else
let argv = System.Environment.GetCommandLineArgs() 
let SetCulture() = 
  if argv.Length > 2 && argv.[1] = "--culture" then  begin
    let cultureString = argv.[2] in 
    let culture = new System.Globalization.CultureInfo(cultureString) in 
    stdout.WriteLine ("Running under culture "+culture.ToString()+"...");
    System.Threading.Thread.CurrentThread.CurrentCulture <-  culture
  end 
  
do SetCulture()    
#endif

(* Some test expressions *)

(* Can we access an F# constructor via a long path *)
let v0 = Microsoft.FSharp.Core.Some("")
let v0b = Microsoft.FSharp.Core.Option.Some("")
let v0c = Microsoft.FSharp.Core.option.Some("")

(* Can we access an F# nullary constructor via a long path *)
let v1 = (Microsoft.FSharp.Core.None : int option)
let v1b = (Microsoft.FSharp.Core.Option.None : int option)
let v1c = (Microsoft.FSharp.Core.option.None : int option)

(* Can we access an F# type name via a long path *)
let v2 = (None : int Microsoft.FSharp.Core.Option)

(* Can we access an F# field name via a long path *)
let v3 = { Microsoft.FSharp.Core.contents = 1 }
let v3b = { Microsoft.FSharp.Core.Ref.contents = 1 }
let v3c = { Microsoft.FSharp.Core.ref.contents = 1 }
let v3d = { contents = 1 }
let v3e = { Ref.contents = 1 }
let v3f = { ref.contents = 1 }
let v3g = { Core.contents = 1 }
let v3h = { Core.Ref.contents = 1 }
let v3i = { Core.ref.contents = 1 }

(* Can we construct a "ref" value *)
let v4 = ref 1

(* Can we access an F# exception constructor via a long path *)
let v5 = (Microsoft.FSharp.Core.MatchFailureException("1",2,2) : exn)

(* Can we construct a "lazy" value *)
let v6 = lazy 1

(* Can we pattern match against a constructor specified via a long path *)
let v7 = 
  match v0 with 
  | Microsoft.FSharp.Core.Some(x) -> x
  | _ -> failwith ""
let v7b2 = 
  match v0 with 
  | option.Some(x) -> x
  | _ -> failwith ""
let v7b3 = 
  match v0 with 
  | Option.Some(x) -> x
  | _ -> failwith ""
let v7b = 
  match v0 with 
  | Microsoft.FSharp.Core.option.Some(x) -> x
  | _ -> failwith ""
let v7c = 
  match v0 with 
  | Microsoft.FSharp.Core.Option.Some(x) -> x
  | _ -> failwith ""


(* Can we pattern match against a nullary constructor specified via a long path *)
let v8 = 
  match v1 with 
  | Microsoft.FSharp.Core.None -> 1
  | _ -> failwith ""
let v8b = 
  match v1 with 
  | Microsoft.FSharp.Core.Option.None -> 1
  | _ -> failwith ""
let v8c = 
  match v1 with 
  | Microsoft.FSharp.Core.option.None -> 1
  | _ -> failwith ""

(* Can we pattern match against an exception constructor specified via a long path *)
let v9 = 
  match v5 with 
  | Microsoft.FSharp.Core.MatchFailureException _ -> 1
  | _ -> 2


(* Can we access an F# constructor via a long path *)
let v10 = Microsoft.FSharp.Core.Some(1)


(* Can we pattern match against a constructor specified via a long path *)
let v11 = 
  match v10 with 
  | Microsoft.FSharp.Core.Some(x) -> x
  | _ -> failwith ""

let v12 = 
  match v10 with 
  | Microsoft.FSharp.Core.Some(x) -> x
  | _ -> failwith ""

let v13 = Microsoft.FSharp.Core.Some(1)

#if Portable
#else
(* check lid setting bug *)

open System.Diagnostics
let doubleLidSetter () =
  let p : Process = new Process() in
  p.StartInfo.set_FileName("child.exe"); // OK
  p.StartInfo.FileName <- "child.exe";   // was not OK, now fixed
  ()
#endif

module NameResolutionExample1Bug1218 = begin
    type S = 
        | A
        | B 
        with 
          static member C = "ONE"
        end

    type U = class 
        [<DefaultValue>]
        static val mutable private d : int
        static member D with get() = U.d and set v = U.d <- v
    end

    type s = 
        | S 
        | U
        with 
          member x.A = 1
          member x.C = 1
          member x.D = "1"
        end

    let _ = (S.A : S)  // the type constraint proves that this currently resolves to type S, constructor A
    let _ = (S.C : string)  // the type constraint proves that this currently resolves to type S, property C
    let _ = (U.D : int)  // the type constraint proves that this currently resolves to type S, static value D
end

module NameResolutionExample2Bug1218 = begin
    type S = 
        | A
        | B 

    type s = class 
        new () = { } 
        member x.A = 1
    end
    
    let S : s = new s()
    
    let _ = (S.A : int)  // the type constraint proves that this currently resolves to value S, member A, i.e. value lookups take precedence over types
    let _ = (fun (S : s) -> S.A : int)  // the type constraint proves that this currently resolves to value S, member A, i.e. value lookups take precedence over types
end

module LookupStaticFieldInType = begin

    type TypeInfoResult = 
       | Unknown = 0
       // .NET reference types
       | Null_CanArise_Allowed_NotTrueValue = 1
       // F# types with [<PermitNull>]
       | Null_CanArise_Allowed_TrueValue = 2
       // F# types
       | Null_CanArise_NotAllowed = 3
       // structs
       | Null_Never = 4
       

    type TypeInfo<'a>() = class
       [<DefaultValue>]
       static val mutable private info : TypeInfoResult
       static member TypeInfo 
                with get() = 
                 if TypeInfo<'a>.info = TypeInfoResult.Unknown then (
                     let nullness = 
                         let ty = typeof<'a> in
                         if ty.IsValueType 
                         then TypeInfoResult.Null_Never else
                         let mappingAttrs = ty.GetCustomAttributes(typeof<CompilationMappingAttribute>,false) in
                         if mappingAttrs.Length = 0 
                         then TypeInfoResult.Null_CanArise_Allowed_NotTrueValue
                         else                      
                             let reprAttrs = ty.GetCustomAttributes(typeof<CompilationRepresentationAttribute>,false) in
                             if reprAttrs.Length = 0 
                             then TypeInfoResult.Null_CanArise_NotAllowed 
                             else
                                 let reprAttr = reprAttrs.[0]  in
                                 let reprAttr = (failwith "" : CompilationRepresentationAttribute ) in
                                 if true 
                                 then TypeInfoResult.Null_CanArise_NotAllowed
                                 else TypeInfoResult.Null_CanArise_Allowed_TrueValue in
                     // The lookup on this line was failing to resolve
                     TypeInfo<'a>.info <- nullness
                 );
                 TypeInfo<'a>.info
    end
end


module TestsForUsingTypeNamesAsValuesWhenTheTypeHasAConstructor = begin
    // All the tests in this file relate to FSharp 1.0 bug 4379:    Testing fix name resolution is weird when T constructor shadows method of same name
    module Test0 = begin
        let x = obj()

        let foo x = x + 1

        type foo() = class end

        let y = foo()  // still does not compile, and this is not shadowing!

        let x2 = ref 1
    end
    module Test1 =  begin
        let _ = Set<int> [3;4;5]
        let _ = Set [3;4;5]
    end
    module Test2 =  begin
        type Set() = class
            let x = 1
            static member Foo = 1
        end
            
        type Set<'T,'Tag>() =  class
            let x = 1
            static member Foo = 1
        end

        let _ = Set()
        //Set<>()
        let _ = Set<_> [3;4;5]
        let _ = Set<int,int>()
        let _ = Set<int,int>.Foo

    //let x : obj list = [ 1;2;3;4]
    end
    module Test3a =   begin
        let f()  = 
            float 1.0 |> ignore;
            decimal 1.0 |> ignore;
            float32 1.0 |> ignore;
            sbyte 1.0 |> ignore;
            byte 1.0 |> ignore;
            int16 1.0 |> ignore;
            uint16 1.0 |> ignore;
            int32 1.0 |> ignore;
            int64 1.0 |> ignore;
            uint32 1.0 |> ignore;
            uint64 1.0 |> ignore;
            string 1.0 |> ignore;
            unativeint 1.0 |> ignore;
            nativeint 1.0 |> ignore

    end
    module Test3b =  begin
        open Microsoft.FSharp.Core
        let f()  = 
            float 1.0 |> ignore;
            decimal 1.0 |> ignore;
            float32 1.0 |> ignore;
            sbyte 1.0 |> ignore;
            byte 1.0 |> ignore;
            int16 1.0 |> ignore;
            uint16 1.0 |> ignore;
            int32 1.0 |> ignore;
            int64 1.0 |> ignore;
            uint32 1.0 |> ignore;
            uint64 1.0 |> ignore;
            string 1.0 |> ignore;
            unativeint 1.0 |> ignore;
            nativeint 1.0 |> ignore;
    end 
    module Test3c =  begin
        open Microsoft.FSharp.Core.Operators
        open Microsoft.FSharp.Core
        let x3 = float 1.0
    end

    module Test3d =  begin
        open System
            // This is somewhat perversely using the type name 'decimal' as a constructor, but it is legal
        let x3 = Decimal 1.0
        let x4 = decimal 1.0
    end
    module Test3e =  begin
        let x3 = System.Decimal 1.0
        let x4 = decimal 1.0
    end
    module Test3f =  begin
            // This is somewhat perversely using the type name 'decimal' as a constructor, but it is legal
            Microsoft.FSharp.Core.decimal 1.0 |> ignore;
            // This is somewhat perversely using the type name 'string' as a constructor
            Microsoft.FSharp.Core.string ('3',100) |> ignore
    end
    module Test7 =  begin
        open System
        let x3 = Decimal 1.0
        let x4 = decimal 1.0
    end

    module TestValuesGetAddedAfterTypes =  begin

        module M = begin
            type string = System.String
      
            let string (x:int,y:int) = "1"
        end
        
        open M
        
        let x = string (1,1)

    end
    module TestValuesGetAddedAfterTypes2 =  begin

        module M = begin
            let string (x:int,y:int) = "1"

            type string = System.String
        end
        open M
        
        let x = string (1,1)

    end
    module TestAUtoOpenNestedModulesGetAddedAfterTypes2 =  begin

        module M = begin
            type string = System.String
            [<AutoOpen>]
            module M2 = begin
                let string (v:int,y:int) = 3
            end
        end
        open M
        
        let x = string (1,1)

    end
    module TestAUtoOpenNestedModulesGetAddedAfterVals =  begin

        module M = begin
            let string(x:string,y:string,z:string) = 23
            [<AutoOpen>]
            module M2 = begin
                let string (v:int,y:int) = 3
            end
        end
        open M
        
        // The module M2 gets auto-opened after the values for "M" get added , hence this typechecks OK
        let x = string (1,1)

    end
    module TestAUtoOpenNestedModulesGetAddedInOrder =  begin

        module M = begin
            let string(x:string,y:string,z:string) = 23
            [<AutoOpen>]
            module M2 = begin
                let string (v:int,y:int) = 3
            end
            [<AutoOpen>]
            module M3 =  begin
                let string (v:int,y:int,z:int) = 3
            end
        end
        open M
        
        // The auto-open modules get added in declaration order 
        let x = string (1,1,3)

    end 
    // AutoOpen modules get auto-opened in the order they appear in the referenced signature
    module TestAUtoOpenNestedModulesGetAddedInOrder_ReverseAlphabetical = begin

        module M = begin
            let string(x:string,y:string,z:string) = 23
            [<AutoOpen>]
            module M3 = begin
                let string (v:int,y:int) = 3
            end
            [<AutoOpen>]
            module M2 =  begin
                let string (v:int,y:int,z:int) = 3
            end
        end
        open M
        
        // The auto-open modules get added in declaration order 
        let x = string (1,1,3)
    end

end


#if ALL_IN_ONE
let RUN() = !failures
#else
let aa =
  match !failures with 
  | [] -> 
      stdout.WriteLine "Test Passed"
      System.IO.File.WriteAllText("test.ok","ok")
      exit 0
  | _ -> 
      stdout.WriteLine "Test Failed"
      exit 1
#endif