You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
4.1 compiler generates invalid programs for structs which use measure types #2373
The combination of a struct, a measure type and no optimization can create incorrect programs which fail verification by peverify, and raise InvalidProgramException when run.
Compile the code without optimization using version 4.1 of the F# compiler:
PS> &"C:\Program Files (x86)\Microsoft SDKs\F#\4.1\Framework\v4.0\Fsc.exe" --optimize- .\Program.fs
Microsoft (R) F# Compiler version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
Run the code:
PS> .\Program.exe
Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program.
at [email protected]()
at Program.main(String[] argv)
Verify the code:
PS> peverify.exe Program.exe
Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.
[IL]: Error: [...\Program.exe : Program::main][offset 0x00000005][found ref 'Program+x@7'][expected value 'Program+AStruct'] Unexpected type on the stack.
[IL]: Error: [...\Program.exe : Program+x@7::.ctor][offset 0x00000009] Return from .ctor when this is uninitialized.
2 Error(s) Verifying Program.exe
Expected behavior
Program runs without crashing, and verification with peverify completes without reporting any errors.
Actual behavior
The program crashes with an InvalidProgramException, and verification with peverify reports 2 errors.
Known workarounds
Either:
Use a class instead of a struct
don't use a measure type
Turn on optimization
Related information
Operating system: Windows 10 (Anniversary Update)
.NET Runtime, CoreCLR or Mono Version: .Net 4.6.2
Indications of severity: prevents use of measure types with structs
The original failing code above used an implicitly dimensionless unit of measure:
typeAStruct<[<Measure>]'u>=structend[<EntryPoint>]letmain argv =letx= AStruct()// This doesn't work0
Implicitly using a measure type also fails:
typeAStruct<[<Measure>]'u>=structvalX:float<'u>end[<Measure>]typeu[<EntryPoint>]letmain argv =letx= AStruct()// This also doesn't work
printfn "%g"(x.X +1.0<u>)0
However, explicitly specifying a dimensionless unit works:
typeAStruct<[<Measure>]'u>=structend[<EntryPoint>]letmain argv =letx= AStruct<1>()// This is fine0
And explicitly providing a measure type works:
typeAStruct<[<Measure>]'u>=structend[<Measure>]typeu[<EntryPoint>]letmain argv =letx= AStruct<u>()// This is fine0
The reason that the explicit versions continue to work after the commit which added isLocalTypeFunc to the match expression ((1) below), is that they are passed as Const not TyLambda, so their path of execution is unchanged i.e. they pass through (2) below:
andGenBindRhs cenv cgbuf eenv sp (vspec:Val)e =match e with| Expr.TyLambda _| Expr.Lambda _->letisLocalTypeFunc= IsNamedLocalTypeFuncVal cenv.g vspec e
match e, isLocalTypeFunc (* (1) *)with| Expr.TyLambda(_, tyargs, body,_,_),truewhen(
tyargs |> List.forall (fun tp -> tp.IsErased)&&(match StorageForVal vspec.Range vspec eenv with Local _->true|_->false))->// type lambda with erased type arguments that is stored as local variable (not method or property)- inline body
GenExpr cenv cgbuf eenv sp body Continue
|_->letselfv=if isLocalTypeFunc then None else Some (mkLocalValRef vspec)
GenLambda cenv cgbuf eenv isLocalTypeFunc selfv e Continue
|_->// (2)
GenExpr cenv cgbuf eenv sp e Continue
I looked into this a bit more. Here are some other ways that work to fix this:
type AStruct<[<Measure>]'u> =
struct
end
[<EntryPoint>]
let main argv =
let x = fun () -> AStruct() // This works, you can also do: let x () = AStruct()
printfn "%A" (x())
0
or
type AStruct<[<Measure>]'u> =
struct
val X : float<'u>
end
[<Measure>] type u
[<EntryPoint>]
let main argv =
let x = AStruct().X + 1.0<u> // This also works
0
As far as I can see it seems quite specific. I'm making an attempt to fix it here: #2404
Activity
BillHally commentedon Feb 6, 2017
This is related to 0fe9edd - reverting the changes it made prevents the errors described above from happening.
I'll investigate further tomorrow.
dsyme commentedon Feb 7, 2017
@cartermp This is a regression unfortunately
cartermp commentedon Feb 7, 2017
Noted. Added to the list to document.
BillHally commentedon Feb 8, 2017
A few more observations:
The original failing code above used an implicitly dimensionless unit of measure:
Implicitly using a measure type also fails:
However, explicitly specifying a dimensionless unit works:
And explicitly providing a measure type works:
The reason that the explicit versions continue to work after the commit which added
isLocalTypeFuncto the match expression ((1)below), is that they are passed asConstnotTyLambda, so their path of execution is unchanged i.e. they pass through(2)below:liboz commentedon Feb 10, 2017
I looked into this a bit more. Here are some other ways that work to fix this:
or
As far as I can see it seems quite specific. I'm making an attempt to fix it here: #2404
Fix for #2373 (#2404)