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

Commit1f07561

Browse files
jwostyKevinRansom
authored andcommitted
[Complete] Make enum pattern matches print field names (dotnet#4620)
* Make incomplete pattern match for enums return enum field names if possible(initial messy code)* Simplify and correct logic* Update test* Remove comment* Fix fsharpqa test* Remove comment
1 parentfa1c97f commit1f07561

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

‎src/fsharp/NicePrint.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,8 @@ module private PrintData =
19231923
letfields= tc.TrueInstanceFieldsAsList
19241924
letlay fs x=(wordL(tagRecordField fs.rfield_id.idText)^^ sepL(tagPunctuation"="))---(dataExprL denv x)
19251925
leftL(tagPunctuation"{")^^ semiListL(List.map2 lay fields xs)^^ rightL(tagPunctuation"}")
1926+
| Expr.Op(TOp.ValFieldGet(RecdFieldRef.RFRef(tcref, name)),_,_,_)->
1927+
(layoutTyconRef denv tcref)^^ sepL(tagPunctuation".")^^ wordL(tagField name)
19261928
| Expr.Op(TOp.Array,[_],xs,_)-> leftL(tagPunctuation"[|")^^ semiListL(dataExprsL denv xs)^^ RightL.rightBracketBar
19271929
|_-> wordL(tagPunctuation"?")
19281930
andprivatedataExprsL denv xs= List.map(dataExprL denv) xs

‎src/fsharp/PatternMatchCompilation.fs‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,22 +233,23 @@ let RefuteDiscrimSet g m path discrims =
233233
|_->
234234
raise CannotRefute)
235235

236-
letcoversKnownEnumValues=
237-
match tryDestAppTy g tywith
238-
| Some tcrefwhen tcref.IsEnumTycon->
239-
letknownValues=
240-
tcref.AllFieldsArray|> Array.choose(fun f->
241-
match f.rfield_const, f.rfield_staticwith
242-
| Some value,true-> Some value
243-
|_,_-> None)
244-
Array.forall(fun ev-> consts.Contains ev) knownValues
245-
|_->false
246-
247-
(* REVIEW: we could return a better enumeration literal field here if a field matches one of the enumeration cases*)
248-
249236
match c'with
250237
| None-> raise CannotRefute
251-
| Some c-> Expr.Const(c,m,ty), coversKnownEnumValues
238+
| Some c->
239+
match tryDestAppTy g tywith
240+
| Some tcrefwhen tcref.IsEnumTycon->
241+
// search for an enum value that pattern match (consts) does not contain
242+
letnonCoveredEnumValues=
243+
tcref.AllFieldsArray|> Array.tryFind(fun f->
244+
match f.rfield_constwith
245+
| None->false
246+
| Some fieldValue->(not(consts.Contains fieldValue))&& f.rfield_static)
247+
match nonCoveredEnumValueswith
248+
| None-> Expr.Const(c,m,ty),true
249+
| Some f->
250+
letv= RecdFieldRef.RFRef(tcref, f.rfield_id.idText)
251+
Expr.Op(TOp.ValFieldGet v,[ty],[], m),false
252+
|_-> Expr.Const(c,m,ty),false
252253

253254
|(DecisionTreeTest.UnionCase(ucref1,tinst):: rest)->
254255
letucrefs= ucref1:: List.choose(function DecisionTreeTest.UnionCase(ucref,_)-> Some ucref|_-> None) rest
@@ -693,7 +694,7 @@ let CompilePatternBasic
693694
// Note the input expression has already been evaluated and saved into a variable.
694695
// Hence no need for a new sequence point.
695696
letmbuilder=new MatchBuilder(NoSequencePointAtInvisibleBinding,exprm)
696-
clausesL|> List.iteri(fun _i c-> mbuilder.AddTarget c.Target|> ignore)
697+
clausesL|> List.iteri(fun _i c-> mbuilder.AddTarget c.Target|> ignore)
697698

698699
// Add the incomplete or rethrow match clause on demand, printing a
699700
// warning if necessary (only if it is ever exercised)
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11

2-
neg102.fs(8,14,8,22): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value 'enum<EnumAB> (1)' may indicate a casenot covered by the pattern(s).
2+
neg102.fs(9,14,9,22): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value 'B' may indicate a casenot covered by the pattern(s).
33

4-
neg102.fs(11,14,11,22): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value 'B' may indicate a casenot covered by the pattern(s).
4+
neg102.fs(12,14,12,22): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value '0' may indicate a casenot covered by the pattern(s).
55

6-
neg102.fs(14,14,14,22): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value '0' may indicate a casenot covered by the pattern(s).
6+
neg102.fs(15,14,15,22): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value 'EnumABC.B' may indicate a casenot covered by the pattern(s).
77

8-
neg102.fs(20,14,20,22): typecheck errorFS0104:Enums may take values outside known cases.For example, the value 'enum<EnumAB> (2)' may indicate a casenot covered by the pattern(s).
8+
neg102.fs(18,14,18,22): typecheck errorFS0025: Incomplete pattern matches on this expression.For example, the value 'EnumABC.C' may indicate a casenot covered by the pattern(s).
99

10-
neg102.fs(24,14,24,22): typecheck error FS0104:Enums may take values outside known cases.For example, the value 'Some (enum<EnumAB> (2))' may indicate a casenot covered by the pattern(s).
10+
neg102.fs(22,14,22,22): typecheck error FS0025: Incomplete pattern matches on this expression.For example, the value 'Some (EnumABC.C)' may indicate a casenot covered by the pattern(s).
11+
12+
neg102.fs(29,14,29,22): typecheck error FS0104:Enums may take values outside known cases.For example, the value 'enum<EnumABC> (2)' may indicate a casenot covered by the pattern(s).
13+
14+
neg102.fs(34,14,34,22): typecheck error FS0104:Enums may take values outside known cases.For example, the value 'Some (enum<EnumABC> (2))' may indicate a casenot covered by the pattern(s).
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
moduleM
2-
typeEnumAB= A=0| B=1
2+
typeEnumABC= A=0| B=1| C=42
33
typeUnionAB= A| B
44

55
moduleFS0025=
66
// All of these should emit warning FS0025 ("Incomplete pattern match....")
77

8+
89
letf1=function
9-
|EnumAB.A->"A"
10+
|UnionAB.A->"A"
1011

1112
letf2=function
12-
|UnionAB.A->"A"
13+
|42->"forty-two"
1314

1415
letf3=function
15-
|42->"forty-two"
16+
| EnumABC.A->"A"
17+
18+
letf4=function
19+
| EnumABC.A->"A"
20+
| EnumABC.B->"B"
21+
22+
letf5=function
23+
| Some(EnumABC.A)| Some(EnumABC.B)->"A|B"
24+
| None->"neither"
1625

1726
moduleFS0104=
1827
// These should emit warning FS0104 ("Enums may take values outside of known cases....")
1928

2029
letf1=function
21-
| EnumAB.A->"A"
22-
| EnumAB.B->"B"
30+
| EnumABC.A->"A"
31+
| EnumABC.B->"B"
32+
| EnumABC.C->"C"
2333

2434
letf2=function
25-
| Some(EnumAB.A)->"A"
26-
| Some(EnumAB.B)->"B"
35+
| Some(EnumABC.A)->"A"
36+
| Some(EnumABC.B)->"B"
37+
| Some(EnumABC.C)->"C"
2738
| None->"none"

‎tests/fsharpqa/Source/Conformance/PatternMatching/Expression/W_CounterExampleWithEnum02.fs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// #Regression #Conformance #PatternMatching
22
// Regression test for DevDiv:198999 ("Warning messages for incomplete matches involving enum types are wrong")
33
//<Expects status="warning" span="(14,10-14,18)" id="FS0104">Enums may take values outside known cases\. For example, the value 'enum<T> \(2\)' may indicate a case not covered by the pattern\(s\)\.$</Expects>
4-
//<Expects status="warning" span="(18,10-18,18)">Incomplete pattern matches on this expression\. For example, the value 'enum<T> \(1\)' may indicate a case not covered by the pattern\(s\)\.$</Expects>
5-
//<Expects status="warning" span="(21,10-21,18)">Incomplete pattern matches on this expression\. For example, the value 'enum<T> \(1\)' may indicate a case not covered by the pattern\(s\)\.$</Expects>
6-
//<Expects status="warning" span="(24,10-24,18)">Incomplete pattern matches on this expression\. For example, the value 'enum<T> \(1\)' may indicate a case not covered by the pattern\(s\)\.$</Expects>
7-
//<Expects status="warning" span="(27,10-27,18)">Incomplete pattern matches on this expression\. For example, the value 'enum<T> \(1\)' may indicate a case not covered by the pattern\(s\)\.$</Expects>
8-
//<Expects status="warning" span="(30,10-30,18)">Incomplete pattern matches on this expression\. For example, the value 'enum<T> \(1\)' may indicate a case not covered by the pattern\(s\)\.$</Expects>
4+
//<Expects status="warning" span="(18,10-18,18)">Incomplete pattern matches on this expression\. For example, the value 'T.Y' may indicate a case not covered by the pattern\(s\)\.$</Expects>
5+
//<Expects status="warning" span="(21,10-21,18)">Incomplete pattern matches on this expression\. For example, the value 'T.Y' may indicate a case not covered by the pattern\(s\)\.$</Expects>
6+
//<Expects status="warning" span="(24,10-24,18)">Incomplete pattern matches on this expression\. For example, the value 'T.Y' may indicate a case not covered by the pattern\(s\)\.$</Expects>
7+
//<Expects status="warning" span="(27,10-27,18)">Incomplete pattern matches on this expression\. For example, the value 'T.Y' may indicate a case not covered by the pattern\(s\)\.$</Expects>
8+
//<Expects status="warning" span="(30,10-30,18)">Incomplete pattern matches on this expression\. For example, the value 'T.Y' may indicate a case not covered by the pattern\(s\)\.$</Expects>
99

1010
moduleM
1111

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp