Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit55c4c45

Browse files
TIHanKevinRansom
authored andcommitted
Fixed match expression is evaluated twice if it returns a struct union (dotnet#5720)
* Thanks to Don Syme, we have a fix* Added test
1 parent2948313 commit55c4c45

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

‎src/fsharp/PatternMatchCompilation.fs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,11 @@ let CompilePatternBasic
907907
else
908908
None,true)
909909

910+
andIsCopyableInputExpr origInputExpr=
911+
match origInputExprwith
912+
| Expr.Op(TOp.LValueOp(LByrefGet, v),[],[],_)whennot v.IsMutable->true
913+
|_->false
914+
910915
andChoosePreBinder simulSetOfEdgeDiscrims subexpr=
911916
match simulSetOfEdgeDiscrimswith
912917
// Very simple 'isinst' tests: put the result of 'isinst' in a local variable
@@ -939,7 +944,7 @@ let CompilePatternBasic
939944
letargExpr= GetSubExprOfInput subexpr
940945
letargExpr=
941946
match argExpr,_origInputExprOptwith
942-
| Expr.Val(v1,_,_), Some origInputExprwhen valEq origInputVal v1.Deref-> origInputExpr
947+
| Expr.Val(v1,_,_), Some origInputExprwhen valEq origInputVal v1.Deref&& IsCopyableInputExpr origInputExpr-> origInputExpr
943948
|_-> argExpr
944949
letvOpt,addrExp,_readonly,_writeonly= mkExprAddrOfExprAux gtruefalse NeverMutates argExpr None matchm
945950
match vOptwith

‎tests/FSharp.Core.UnitTests/FSharp.Core/DiscrimantedUnionType.fs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,10 @@ let [<Test>] ``can properly construct a struct union using FSharpValue.MakeUnion
249249
letc2=(fieldVals.[1]:?> int)
250250
Assert.AreEqual(3456, c2)
251251

252+
let [<Test>]``struct unions does optimization correctly on pattern matching``()=
253+
letarr= ResizeArray()
254+
match arr.Add(1); ValueSome()with
255+
| ValueSome()->()
256+
| ValueNone->()
257+
258+
Assert.AreEqual(1, arr.Count)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp