Skip to content

Commit b8335bc

Browse files
committed
Removed Upcast hack due to dotnet#2972
1 parent 919a2a1 commit b8335bc

File tree

3 files changed

+93
-134
lines changed

3 files changed

+93
-134
lines changed

src/fsharp/FSharp.Core/iseq.fs

+34-41
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ namespace Microsoft.FSharp.Collections
102102
// ineffictive **
103103
let inline avoid boolean = match boolean with true -> true | false -> false
104104

105-
module internal Upcast =
106-
// The f# compiler outputs unnecessary unbox.any calls in upcasts. If this functionality
107-
// is fixed with the compiler then these functions can be removed.
108-
let inline seq<'T,'seq when 'seq :> ISeq<'T> and 'seq : not struct> (t:'seq) : ISeq<'T> = (# "" t : ISeq<'T> #)
109-
let inline enumerableNonGeneric<'enumerable when 'enumerable :> IEnumerable and 'enumerable : not struct> (t:'enumerable) : IEnumerable = (# "" t : IEnumerable #)
110-
let inline outOfBand<'outOfBand when 'outOfBand :> IOutOfBand and 'outOfBand : not struct> (t:'outOfBand) : IOutOfBand = (# "" t : IOutOfBand #)
111-
112105
let inline valueComparer<'T when 'T : equality> ()=
113106
let c = HashIdentity.Structural<'T>
114107
{ new IEqualityComparer<Value<'T>> with
@@ -119,7 +112,7 @@ namespace Microsoft.FSharp.Collections
119112
let empty<'T> = Microsoft.FSharp.Collections.SeqComposition.Core.EmptyEnumerable<'T>.Instance
120113

121114
[<CompiledName("Singleton")>]
122-
let singleton x = Upcast.seq (new SingletonEnumerable<_>(x))
115+
let singleton<'T> (x:'T) : ISeq<'T> = upcast (new SingletonEnumerable<_>(x))
123116

124117
/// wraps a ResizeArray in the ISeq framework. Care must be taken that the underlying ResizeArray
125118
/// is not modified whilst it can be accessed as the ISeq, so check on version is performed.
@@ -128,24 +121,24 @@ namespace Microsoft.FSharp.Collections
128121
/// performed in this case. If you want this funcitonality, then use the ofSeq function instead.
129122
[<CompiledName "OfResizeArrayUnchecked">]
130123
let ofResizeArrayUnchecked (source:ResizeArray<'T>) : ISeq<'T> =
131-
Upcast.seq (ThinResizeArrayEnumerable<'T> source)
124+
upcast (ThinResizeArrayEnumerable<'T> source)
132125

133126
[<CompiledName "OfArray">]
134127
let ofArray (source:array<'T>) : ISeq<'T> =
135128
checkNonNull "source" source
136-
Upcast.seq (ThinArrayEnumerable<'T> source)
129+
upcast (ThinArrayEnumerable<'T> source)
137130

138131
[<CompiledName "OfList">]
139132
let ofList (source:list<'T>) : ISeq<'T> =
140-
Upcast.seq source
133+
upcast source
141134

142135
[<CompiledName "OfSeq">]
143136
let ofSeq (source:seq<'T>) : ISeq<'T> =
144137
match source with
145138
| :? ISeq<'T> as seq -> seq
146139
| :? array<'T> as array -> ofArray array
147140
| null -> nullArg "source"
148-
| _ -> Upcast.seq (ThinEnumerable<'T> source)
141+
| _ -> upcast (ThinEnumerable<'T> source)
149142

150143
[<CompiledName "Average">]
151144
let inline average (source:ISeq<'T>) =
@@ -187,7 +180,7 @@ namespace Microsoft.FSharp.Collections
187180
this.Result <- value
188181
else
189182
this.State._2 <- true
190-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
183+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
191184
Unchecked.defaultof<_> (* return value unused in Fold context *)
192185

193186
override this.OnComplete _ =
@@ -213,25 +206,25 @@ namespace Microsoft.FSharp.Collections
213206
if this.State.MoveNext() then
214207
this.Result <- folder this.Result value this.State.Current
215208
else
216-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
209+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
217210
Unchecked.defaultof<_> (* return value unused in Fold context *)
218211

219212
override this.OnComplete _ = ()
220213
override this.OnDispose () = this.State.Dispose () })
221214

222215
[<CompiledName "Unfold">]
223216
let unfold (generator:'State->option<'T * 'State>) (state:'State) : ISeq<'T> =
224-
Upcast.seq (new UnfoldEnumerable<'T,'T,'State>(generator, state, IdentityFactory.Instance, 1))
217+
upcast (new UnfoldEnumerable<'T,'T,'State>(generator, state, IdentityFactory.Instance, 1))
225218

226219
[<CompiledName "InitializeInfinite">]
227220
let initInfinite<'T> (f:int->'T) : ISeq<'T> =
228-
Upcast.seq (new InitEnumerableDecider<'T>(Nullable (), f, 1))
221+
upcast (new InitEnumerableDecider<'T>(Nullable (), f, 1))
229222

230223
[<CompiledName "Initialize">]
231224
let init<'T> (count:int) (f:int->'T) : ISeq<'T> =
232225
if count < 0 then invalidArgInputMustBeNonNegative "count" count
233226
elif count = 0 then empty else
234-
Upcast.seq (new InitEnumerableDecider<'T>(Nullable count, f, 1))
227+
upcast (new InitEnumerableDecider<'T>(Nullable count, f, 1))
235228

236229
[<CompiledName "Iterate">]
237230
let inline iter f (source:ISeq<'T>) =
@@ -249,7 +242,7 @@ namespace Microsoft.FSharp.Collections
249242
if this.State.MoveNext() then
250243
f value this.State.Current
251244
else
252-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
245+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
253246
Unchecked.defaultof<_> (* return value unused in Fold context *)
254247

255248
override this.OnComplete _ = ()
@@ -265,7 +258,7 @@ namespace Microsoft.FSharp.Collections
265258
this.State._1 <- this.State._1 + 1
266259
Unchecked.defaultof<_>
267260
else
268-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
261+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
269262
Unchecked.defaultof<_>
270263
override this.OnComplete _ = ()
271264
override this.OnDispose () = this.State._2.Dispose () })
@@ -276,7 +269,7 @@ namespace Microsoft.FSharp.Collections
276269
{ new Folder<'T, Option<'T>> (None) with
277270
override this.ProcessNext value =
278271
this.Result <- Some value
279-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
272+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
280273
Unchecked.defaultof<_> (* return value unused in Fold context *) })
281274

282275
[<CompiledName "Head">]
@@ -310,7 +303,7 @@ namespace Microsoft.FSharp.Collections
310303
override this.ProcessNext value =
311304
if f value then
312305
this.Result <- true
313-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
306+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
314307
Unchecked.defaultof<_> (* return value unused in Fold context *) })
315308

316309
[<CompiledName "Exists2">]
@@ -321,9 +314,9 @@ namespace Microsoft.FSharp.Collections
321314
if this.State.MoveNext() then
322315
if predicate value this.State.Current then
323316
this.Result <- true
324-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
317+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
325318
else
326-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
319+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
327320
Unchecked.defaultof<_> (* return value unused in Fold context *)
328321

329322
override this.OnComplete _ = ()
@@ -336,7 +329,7 @@ namespace Microsoft.FSharp.Collections
336329
override this.ProcessNext value =
337330
if element = value then
338331
this.Result <- true
339-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
332+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
340333
Unchecked.defaultof<_> (* return value unused in Fold context *) })
341334

342335
[<CompiledName "ForAll">]
@@ -346,7 +339,7 @@ namespace Microsoft.FSharp.Collections
346339
override this.ProcessNext value =
347340
if not (predicate value) then
348341
this.Result <- false
349-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
342+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
350343
Unchecked.defaultof<_> (* return value unused in Fold context *) })
351344

352345
[<CompiledName "ForAll2">]
@@ -357,9 +350,9 @@ namespace Microsoft.FSharp.Collections
357350
if this.State.MoveNext() then
358351
if not (predicate value this.State.Current) then
359352
this.Result <- false
360-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
353+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
361354
else
362-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
355+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
363356
Unchecked.defaultof<_> (* return value unused in Fold context *)
364357

365358
override this.OnComplete _ = ()
@@ -447,12 +440,12 @@ namespace Microsoft.FSharp.Collections
447440
override this.ProcessNext value =
448441
if not (this.State.MoveNext()) then
449442
this.Result <- 1
450-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
443+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
451444
else
452445
let c = f value this.State.Current
453446
if c <> 0 then
454447
this.Result <- c
455-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
448+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
456449
Unchecked.defaultof<_> (* return value unused in Fold context *)
457450
override this.OnComplete _ =
458451
if this.Result = 0 && this.State.MoveNext() then
@@ -597,7 +590,7 @@ namespace Microsoft.FSharp.Collections
597590

598591
[<CompiledName("Concat")>]
599592
let concat (sources:ISeq<#ISeq<'T>>) : ISeq<'T> =
600-
Upcast.seq (ThinConcatEnumerable (sources, id))
593+
upcast (ThinConcatEnumerable (sources, id))
601594

602595
[<CompiledName "Scan">]
603596
let inline scan (folder:'State->'T->'State) (initialState:'State) (source:ISeq<'T>) :ISeq<'State> =
@@ -788,7 +781,7 @@ namespace Microsoft.FSharp.Collections
788781
match f value with
789782
| (Some _) as some ->
790783
this.Result <- some
791-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
784+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
792785
| None -> ()
793786
Unchecked.defaultof<_> (* return value unused in Fold context *) })
794787

@@ -799,7 +792,7 @@ namespace Microsoft.FSharp.Collections
799792
override this.ProcessNext value =
800793
if f value then
801794
this.Result <- Some value
802-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
795+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
803796
Unchecked.defaultof<_> (* return value unused in Fold context *) })
804797

805798
[<CompiledName "TryFindIndex">]
@@ -810,7 +803,7 @@ namespace Microsoft.FSharp.Collections
810803
override this.ProcessNext value =
811804
if predicate value then
812805
this.Result <- Some this.State
813-
(Upcast.outOfBand this).StopFurtherProcessing pipeIdx
806+
(this :> IOutOfBand).StopFurtherProcessing pipeIdx
814807
else
815808
this.State <- this.State + 1
816809
Unchecked.defaultof<_> (* return value unused in Fold context *) })
@@ -874,11 +867,11 @@ namespace Microsoft.FSharp.Collections
874867
let append (source1:ISeq<'T>) (source2: ISeq<'T>) : ISeq<'T> =
875868
match source1 with
876869
| :? EnumerableBase<'T> as s -> s.Append source2
877-
| _ -> Upcast.seq (new AppendEnumerable<_>([source2; source1]))
870+
| _ -> upcast (new AppendEnumerable<_>([source2; source1]))
878871

879872
[<CompiledName "Delay">]
880-
let delay (delayed:unit->ISeq<'T>) =
881-
Upcast.seq (DelayedEnumerable (delayed, 1))
873+
let delay (delayed:unit->ISeq<'T>) : ISeq<'T> =
874+
upcast (DelayedEnumerable (delayed, 1))
882875

883876
module internal GroupBy =
884877
let inline private impl (comparer:IEqualityComparer<'SafeKey>) (keyf:'T->'SafeKey) (getKey:'SafeKey->'Key) (source:ISeq<'T>) =
@@ -1094,7 +1087,7 @@ namespace Microsoft.FSharp.Collections
10941087
else
10951088
None)
10961089

1097-
let cached = Upcast.seq (new UnfoldEnumerable<'T,'T,int>(unfolding, 0, IdentityFactory.Instance, 1))
1090+
let cached : ISeq<'T> = upcast (new UnfoldEnumerable<'T,'T,int>(unfolding, 0, IdentityFactory.Instance, 1))
10981091

10991092
interface System.IDisposable with
11001093
member __.Dispose() =
@@ -1111,7 +1104,7 @@ namespace Microsoft.FSharp.Collections
11111104
member __.GetEnumerator() = cached.GetEnumerator()
11121105

11131106
interface System.Collections.IEnumerable with
1114-
member __.GetEnumerator() = (Upcast.enumerableNonGeneric cached).GetEnumerator()
1107+
member __.GetEnumerator() = (cached:>IEnumerable).GetEnumerator()
11151108

11161109
interface ISeq<'T> with
11171110
member __.PushTransform next = cached.PushTransform next
@@ -1121,7 +1114,7 @@ namespace Microsoft.FSharp.Collections
11211114

11221115
[<CompiledName("Cache")>]
11231116
let cache (source:ISeq<'T>) : ISeq<'T> =
1124-
Upcast.seq (new CachedSeq<_> (source))
1117+
upcast (new CachedSeq<_> (source))
11251118

11261119
[<CompiledName("Collect")>]
11271120
let collect f sources = map f sources |> concat
@@ -1140,9 +1133,9 @@ namespace Microsoft.FSharp.Collections
11401133
| _ -> Microsoft.FSharp.Primitives.Basics.List.ofISeq source
11411134

11421135
[<CompiledName("Replicate")>]
1143-
let replicate count x =
1136+
let replicate<'T> count (x:'T) : ISeq<'T> =
11441137
if count < 0 then raise (ArgumentOutOfRangeException "count")
1145-
Upcast.seq (new InitEnumerable<'T,'T>(Nullable count, (fun _ -> x), IdentityFactory.Instance, 1))
1138+
upcast (new InitEnumerable<'T,'T>(Nullable count, (fun _ -> x), IdentityFactory.Instance, 1))
11461139

11471140
[<CompiledName("IsEmpty")>]
11481141
let isEmpty (source : ISeq<'T>) =

0 commit comments

Comments
 (0)