"Fossies" - the Fresh Open Source Software Archive

Member "fsharp-4.5/tests/fsharp/core/longnames/test.fsx" (5 Sep 2018, 16060 Bytes) of package /linux/misc/mono-sources/fsharp/fsharp-4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) F# source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test.fsx": 4.1.33_vs_4.5.

    1 // #Conformance #ObjectConstructors 
    2 #if TESTS_AS_APP
    3 module Core_longnames
    4 #endif
    5 let failures = ref []
    6 
    7 let report_failure (s : string) = 
    8     stderr.Write" NO: "
    9     stderr.WriteLine s
   10     failures := !failures @ [s]
   11 
   12 let test (s : string) b = 
   13     stderr.Write(s)
   14     if b then stderr.WriteLine " OK"
   15     else report_failure (s)
   16 
   17 let check s b1 b2 = test s (b1 = b2)
   18 
   19 (* Some test expressions *)
   20 
   21 (* Can we access an F# constructor via a long path *)
   22 let v0 = Microsoft.FSharp.Core.Some("")
   23 let v0b = Microsoft.FSharp.Core.Option.Some("")
   24 let v0c = Microsoft.FSharp.Core.option.Some("")
   25 
   26 (* Can we access an F# nullary constructor via a long path *)
   27 let v1 = (Microsoft.FSharp.Core.None : int option)
   28 let v1b = (Microsoft.FSharp.Core.Option.None : int option)
   29 let v1c = (Microsoft.FSharp.Core.option.None : int option)
   30 
   31 (* Can we access an F# type name via a long path *)
   32 let v2 = (None : int Microsoft.FSharp.Core.Option)
   33 
   34 (* Can we access an F# field name via a long path *)
   35 let v3 = { Microsoft.FSharp.Core.contents = 1 }
   36 let v3b = { Microsoft.FSharp.Core.Ref.contents = 1 }
   37 let v3c = { Microsoft.FSharp.Core.ref.contents = 1 }
   38 let v3d = { contents = 1 }
   39 let v3e = { Ref.contents = 1 }
   40 let v3f = { ref.contents = 1 }
   41 let v3g = { Core.contents = 1 }
   42 let v3h = { Core.Ref.contents = 1 }
   43 let v3i = { Core.ref.contents = 1 }
   44 
   45 (* Can we construct a "ref" value *)
   46 let v4 = ref 1
   47 
   48 (* Can we access an F# exception constructor via a long path *)
   49 let v5 = (Microsoft.FSharp.Core.MatchFailureException("1",2,2) : exn)
   50 
   51 (* Can we construct a "lazy" value *)
   52 let v6 = lazy 1
   53 
   54 (* Can we pattern match against a constructor specified via a long path *)
   55 let v7 = 
   56   match v0 with 
   57   | Microsoft.FSharp.Core.Some(x) -> x
   58   | _ -> failwith ""
   59 let v7b2 = 
   60   match v0 with 
   61   | option.Some(x) -> x
   62   | _ -> failwith ""
   63 let v7b3 = 
   64   match v0 with 
   65   | Option.Some(x) -> x
   66   | _ -> failwith ""
   67 let v7b = 
   68   match v0 with 
   69   | Microsoft.FSharp.Core.option.Some(x) -> x
   70   | _ -> failwith ""
   71 let v7c = 
   72   match v0 with 
   73   | Microsoft.FSharp.Core.Option.Some(x) -> x
   74   | _ -> failwith ""
   75 
   76 
   77 (* Can we pattern match against a nullary constructor specified via a long path *)
   78 let v8 = 
   79   match v1 with 
   80   | Microsoft.FSharp.Core.None -> 1
   81   | _ -> failwith ""
   82 let v8b = 
   83   match v1 with 
   84   | Microsoft.FSharp.Core.Option.None -> 1
   85   | _ -> failwith ""
   86 let v8c = 
   87   match v1 with 
   88   | Microsoft.FSharp.Core.option.None -> 1
   89   | _ -> failwith ""
   90 
   91 (* Can we pattern match against an exception constructor specified via a long path *)
   92 let v9 = 
   93   match v5 with 
   94   | Microsoft.FSharp.Core.MatchFailureException _ -> 1
   95   | _ -> 2
   96 
   97 
   98 (* Can we access an F# constructor via a long path *)
   99 let v10 = Microsoft.FSharp.Core.Some(1)
  100 
  101 
  102 (* Can we pattern match against a constructor specified via a long path *)
  103 let v11 = 
  104   match v10 with 
  105   | Microsoft.FSharp.Core.Some(x) -> x
  106   | _ -> failwith ""
  107 
  108 let v12 = 
  109   match v10 with 
  110   | Microsoft.FSharp.Core.Some(x) -> x
  111   | _ -> failwith ""
  112 
  113 let v13 = Microsoft.FSharp.Core.Some(1)
  114 
  115 #if !NETCOREAPP1_0
  116 (* check lid setting bug *)
  117 
  118 open System.Diagnostics
  119 let doubleLidSetter () =
  120   let p : Process = new Process() in
  121   p.StartInfo.set_FileName("child.exe"); // OK
  122   p.StartInfo.FileName <- "child.exe";   // was not OK, now fixed
  123   ()
  124 #endif
  125 
  126 module NameResolutionExample1Bug1218 = begin
  127     type S = 
  128         | A
  129         | B 
  130         with 
  131           static member C = "ONE"
  132         end
  133 
  134     type U = class 
  135         [<DefaultValue>]
  136         static val mutable private d : int
  137         static member D with get() = U.d and set v = U.d <- v
  138     end
  139 
  140     type s = 
  141         | S 
  142         | U
  143         with 
  144           member x.A = 1
  145           member x.C = 1
  146           member x.D = "1"
  147         end
  148 
  149     let _ = (S.A : S)  // the type constraint proves that this currently resolves to type S, constructor A
  150     let _ = (S.C : string)  // the type constraint proves that this currently resolves to type S, property C
  151     let _ = (U.D : int)  // the type constraint proves that this currently resolves to type S, static value D
  152 end
  153 
  154 module NameResolutionExample2Bug1218 = begin
  155     type S = 
  156         | A
  157         | B 
  158 
  159     type s = class 
  160         new () = { } 
  161         member x.A = 1
  162     end
  163     
  164     let S : s = new s()
  165     
  166     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
  167     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
  168 end
  169 
  170 module LookupStaticFieldInType = begin
  171 
  172     type TypeInfoResult = 
  173        | Unknown = 0
  174        // .NET reference types
  175        | Null_CanArise_Allowed_NotTrueValue = 1
  176        // F# types with [<PermitNull>]
  177        | Null_CanArise_Allowed_TrueValue = 2
  178        // F# types
  179        | Null_CanArise_NotAllowed = 3
  180        // structs
  181        | Null_Never = 4
  182        
  183 
  184     type TypeInfo<'a>() = class
  185        [<DefaultValue>]
  186        static val mutable private info : TypeInfoResult
  187        static member TypeInfo 
  188                 with get() = 
  189                  if TypeInfo<'a>.info = TypeInfoResult.Unknown then (
  190                      let nullness = 
  191                          let ty = typeof<'a> in
  192                          if ty.IsValueType 
  193                          then TypeInfoResult.Null_Never else
  194                          let mappingAttrs = ty.GetCustomAttributes(typeof<CompilationMappingAttribute>,false) in
  195                          if mappingAttrs.Length = 0 
  196                          then TypeInfoResult.Null_CanArise_Allowed_NotTrueValue
  197                          else                      
  198                              let reprAttrs = ty.GetCustomAttributes(typeof<CompilationRepresentationAttribute>,false) in
  199                              if reprAttrs.Length = 0 
  200                              then TypeInfoResult.Null_CanArise_NotAllowed 
  201                              else
  202                                  let reprAttr = reprAttrs.[0]  in
  203                                  let reprAttr = (failwith "" : CompilationRepresentationAttribute ) in
  204                                  if true 
  205                                  then TypeInfoResult.Null_CanArise_NotAllowed
  206                                  else TypeInfoResult.Null_CanArise_Allowed_TrueValue in
  207                      // The lookup on this line was failing to resolve
  208                      TypeInfo<'a>.info <- nullness
  209                  );
  210                  TypeInfo<'a>.info
  211     end
  212 end
  213 
  214 
  215 module TestsForUsingTypeNamesAsValuesWhenTheTypeHasAConstructor = begin
  216     // 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
  217     module Test0 = begin
  218         let x = obj()
  219 
  220         let foo x = x + 1
  221 
  222         type foo() = class end
  223 
  224         let y = foo()  // still does not compile, and this is not shadowing!
  225 
  226         let x2 = ref 1
  227     end
  228     module Test1 =  begin
  229         let _ = Set<int> [3;4;5]
  230         let _ = Set [3;4;5]
  231     end
  232     module Test2 =  begin
  233         type Set() = class
  234             let x = 1
  235             static member Foo = 1
  236         end
  237             
  238         type Set<'T,'Tag>() =  class
  239             let x = 1
  240             static member Foo = 1
  241         end
  242 
  243         let _ = Set()
  244         //Set<>()
  245         let _ = Set<_> [3;4;5]
  246         let _ = Set<int,int>()
  247         let _ = Set<int,int>.Foo
  248 
  249     //let x : obj list = [ 1;2;3;4]
  250     end
  251     module Test3a =   begin
  252         let f()  = 
  253             float 1.0 |> ignore;
  254             decimal 1.0 |> ignore;
  255             float32 1.0 |> ignore;
  256             sbyte 1.0 |> ignore;
  257             byte 1.0 |> ignore;
  258             int16 1.0 |> ignore;
  259             uint16 1.0 |> ignore;
  260             int32 1.0 |> ignore;
  261             int64 1.0 |> ignore;
  262             uint32 1.0 |> ignore;
  263             uint64 1.0 |> ignore;
  264             string 1.0 |> ignore;
  265             unativeint 1.0 |> ignore;
  266             nativeint 1.0 |> ignore
  267 
  268     end
  269     module Test3b =  begin
  270         open Microsoft.FSharp.Core
  271         let f()  = 
  272             float 1.0 |> ignore;
  273             decimal 1.0 |> ignore;
  274             float32 1.0 |> ignore;
  275             sbyte 1.0 |> ignore;
  276             byte 1.0 |> ignore;
  277             int16 1.0 |> ignore;
  278             uint16 1.0 |> ignore;
  279             int32 1.0 |> ignore;
  280             int64 1.0 |> ignore;
  281             uint32 1.0 |> ignore;
  282             uint64 1.0 |> ignore;
  283             string 1.0 |> ignore;
  284             unativeint 1.0 |> ignore;
  285             nativeint 1.0 |> ignore;
  286     end 
  287     module Test3c =  begin
  288         open Microsoft.FSharp.Core.Operators
  289         open Microsoft.FSharp.Core
  290         let x3 = float 1.0
  291     end
  292 
  293     module Test3d =  begin
  294         open System
  295             // This is somewhat perversely using the type name 'decimal' as a constructor, but it is legal
  296         let x3 = Decimal 1.0
  297         let x4 = decimal 1.0
  298     end
  299     module Test3e =  begin
  300         let x3 = System.Decimal 1.0
  301         let x4 = decimal 1.0
  302     end
  303     module Test3f =  begin
  304             // This is somewhat perversely using the type name 'decimal' as a constructor, but it is legal
  305             Microsoft.FSharp.Core.decimal 1.0 |> ignore;
  306             // This is somewhat perversely using the type name 'string' as a constructor
  307             Microsoft.FSharp.Core.string ('3',100) |> ignore
  308     end
  309     module Test7 =  begin
  310         open System
  311         let x3 = Decimal 1.0
  312         let x4 = decimal 1.0
  313     end
  314 
  315     module TestValuesGetAddedAfterTypes =  begin
  316 
  317         module M = begin
  318             type string = System.String
  319       
  320             let string (x:int,y:int) = "1"
  321         end
  322         
  323         open M
  324         
  325         let x = string (1,1)
  326 
  327     end
  328     module TestValuesGetAddedAfterTypes2 =  begin
  329 
  330         module M = begin
  331             let string (x:int,y:int) = "1"
  332 
  333             type string = System.String
  334         end
  335         open M
  336         
  337         let x = string (1,1)
  338 
  339     end
  340     module TestAUtoOpenNestedModulesGetAddedAfterTypes2 =  begin
  341 
  342         module M = begin
  343             type string = System.String
  344             [<AutoOpen>]
  345             module M2 = begin
  346                 let string (v:int,y:int) = 3
  347             end
  348         end
  349         open M
  350         
  351         let x = string (1,1)
  352 
  353     end
  354     module TestAUtoOpenNestedModulesGetAddedAfterVals =  begin
  355 
  356         module M = begin
  357             let string(x:string,y:string,z:string) = 23
  358             [<AutoOpen>]
  359             module M2 = begin
  360                 let string (v:int,y:int) = 3
  361             end
  362         end
  363         open M
  364         
  365         // The module M2 gets auto-opened after the values for "M" get added , hence this typechecks OK
  366         let x = string (1,1)
  367 
  368     end
  369     module TestAUtoOpenNestedModulesGetAddedInOrder =  begin
  370 
  371         module M = begin
  372             let string(x:string,y:string,z:string) = 23
  373             [<AutoOpen>]
  374             module M2 = begin
  375                 let string (v:int,y:int) = 3
  376             end
  377             [<AutoOpen>]
  378             module M3 =  begin
  379                 let string (v:int,y:int,z:int) = 3
  380             end
  381         end
  382         open M
  383         
  384         // The auto-open modules get added in declaration order 
  385         let x = string (1,1,3)
  386 
  387     end 
  388     // AutoOpen modules get auto-opened in the order they appear in the referenced signature
  389     module TestAUtoOpenNestedModulesGetAddedInOrder_ReverseAlphabetical = begin
  390 
  391         module M = begin
  392             let string(x:string,y:string,z:string) = 23
  393             [<AutoOpen>]
  394             module M3 = begin
  395                 let string (v:int,y:int) = 3
  396             end
  397             [<AutoOpen>]
  398             module M2 =  begin
  399                 let string (v:int,y:int,z:int) = 3
  400             end
  401         end
  402         open M
  403         
  404         // The auto-open modules get added in declaration order 
  405         let x = string (1,1,3)
  406     end
  407 
  408 end
  409 
  410 module Ok1 = 
  411 
  412     module A =
  413         let create() = 1
  414         type Dummy = A | B
  415 
  416 
  417     type A() = 
  418         member x.P = 1
  419 
  420     test "lkneecec09iew1" (typeof<A.Dummy>.FullName.Contains("AModule") )
  421 
  422 
  423 module Ok2 = 
  424 
  425     type A() = 
  426         member x.P = 1
  427 
  428 
  429     module A =
  430         let create() = 1
  431         type Dummy = A | B
  432 
  433     test "lkneecec09iew2" (typeof<A.Dummy>.FullName.Contains("AModule") )
  434 
  435 
  436 module Ok3 = 
  437 
  438     [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  439     module A = 
  440         let create() = 1
  441         type Dummy = A | B
  442 
  443     type A() = 
  444         member x.P = 1
  445 
  446     test "lkneecec09iew3" (typeof<A.Dummy>.FullName.Contains("AModule") )
  447 
  448 
  449 module Ok4 = 
  450 
  451     type A() = 
  452         member x.P = 1
  453 
  454     [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  455     module A = 
  456         let create() = 1
  457         type Dummy = A | B
  458 
  459     test "lkneecec09iew4" (typeof<A.Dummy>.FullName.Contains("AModule") )
  460 
  461 
  462 
  463 module rec Ok5 = 
  464 
  465     module A =
  466         let create() = 1
  467         type Dummy = A | B
  468 
  469 
  470     type A() = 
  471         member x.P = 1
  472 
  473     test "lkneecec09iew5" (typeof<A.Dummy>.FullName.Contains("AModule") )
  474 
  475 
  476 module rec Ok6 = 
  477 
  478     type A() = 
  479         member x.P = 1
  480 
  481 
  482     module A =
  483         let create() = 1
  484         type Dummy = A | B
  485 
  486     test "lkneecec09iew6" (typeof<A.Dummy>.FullName.Contains("AModule") )
  487 
  488 
  489 module rec Ok7 = 
  490 
  491     [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  492     module A = 
  493         let create() = 1
  494         type Dummy = A | B
  495 
  496     type A() = 
  497         member x.P = 1
  498 
  499     test "lkneecec09iew7" (typeof<A.Dummy>.FullName.Contains("AModule") )
  500 
  501 
  502 module rec Ok8 = 
  503 
  504     type A() = 
  505         member x.P = 1
  506 
  507     [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  508     module A = 
  509         let create() = 1
  510         type Dummy = A | B
  511 
  512     test "lkneecec09iew8" (typeof<A.Dummy>.FullName.Contains("AModule") )
  513 
  514 
  515 module Ok9 = 
  516 
  517     type A() = 
  518         member x.P = 1
  519 
  520     type A<'T>() = 
  521         member x.P = 1
  522 
  523     module A = 
  524         let create() = 1
  525         type Dummy = A | B
  526 
  527 
  528     test "lkneecec09iew9" (typeof<A.Dummy>.FullName.Contains("AModule") )
  529 
  530 
  531 module Ok9b = 
  532 
  533     type A<'T>() = 
  534         member x.P = 1
  535 
  536     module A = 
  537         let create() = 1
  538         type Dummy = A | B
  539 
  540     //A<'T> has a type parameter, so appending Module is not necessary.
  541     test "lkneecec09iew9" (not (typeof<A.Dummy>.FullName.Contains("AModule") ) )
  542 
  543 module rec Ok10 = 
  544 
  545     type A() = 
  546         member x.P = 1
  547 
  548     type A<'T>() = 
  549         member x.P = 1
  550 
  551     module A = 
  552         let create() = 1
  553         type Dummy = A | B
  554 
  555     test "lkneecec09iew10" (typeof<A.Dummy>.FullName.Contains("AModule") )
  556 
  557 module Ok11 = 
  558 
  559     type A = int
  560 
  561     module A = 
  562         let create() = 1
  563         type Dummy = A | B
  564 
  565     test "lkneecec09iew11" (typeof<A.Dummy>.FullName.Contains("AModule") )
  566 
  567 module Ok12 = 
  568 
  569     type A = A
  570 
  571     module A = 
  572         let create() = 1
  573         type Dummy = A | B
  574 
  575     test "lkneecec09iew12" (typeof<A.Dummy>.FullName.Contains("AModule") )
  576 
  577 module Ok13 = 
  578 
  579     type A = A of string
  580 
  581     module A = 
  582         let create() = 1
  583         type Dummy = A | B
  584 
  585     test "lkneecec09iew13" (typeof<A.Dummy>.FullName.Contains("AModule") )
  586 
  587 
  588 module Ok14 = 
  589 
  590     module X = 
  591         type A = A of string
  592 
  593     type X.A with 
  594         member x.P = 1
  595 
  596     module A =  // the type definition is an augmentation so doesn't get the suffix
  597         let create() = 1
  598         type Dummy = A | B
  599 
  600     test "lkneecec09iew14" (not (typeof<A.Dummy>.FullName.Contains("AModule") )) 
  601 
  602 module rec Ok15 = 
  603 
  604     open X
  605     
  606     module X = 
  607         type A = A of string
  608 
  609     type A with 
  610         member x.P = 1
  611 
  612     module A =  // the type definition is an augmentation so doesn't get the suffix
  613         let create() = 1
  614         type Dummy = A | B
  615 
  616     test "lkneecec09iew15" (not (typeof<A.Dummy>.FullName.Contains("AModule") )) 
  617 
  618 #if TESTS_AS_APP
  619 let RUN() = !failures
  620 #else
  621 let aa =
  622   match !failures with 
  623   | [] -> 
  624       stdout.WriteLine "Test Passed"
  625       System.IO.File.WriteAllText("test.ok","ok")
  626       exit 0
  627   | _ -> 
  628       stdout.WriteLine "Test Failed"
  629       exit 1
  630 #endif
  631