Skip to content

Commit 8a32b2c

Browse files
committed
Fix some typing issues in AwaitExpr
1 parent 2c9d745 commit 8a32b2c

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

Clojure/Clojure.Source/clojure/clr/async/task/alpha.clj

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
or an (async ...) block.
4343
4444
Usage:
45-
(require '[clojure.clr.async.task.alpha :as t])
4645
(t/await (.ReadAllTextAsync System.IO.File path))"
4746
[task-expr]
4847
`(await* ~task-expr))
@@ -78,8 +77,8 @@
7877
7978
timeout values can be any numeric value (cast to int, milliseconds, -1 = no limit) or a TimeSpan"
8079
([tasks] (Task/WaitAll ^Task/1 (into-array Task tasks)))
81-
([tasks timeout] (Task/WaitAll ^Task/1 (into-array Task tasks) ^int (convert-timeout timeout)))
82-
([tasks timeout cancellation-token] (Task/WaitAll ^Task/1 (into-array Task tasks) (convert-timeout timeout) cancellation-token)))
80+
([tasks timeout] (Task/WaitAll ^Task/1 (into-array Task tasks) (int (convert-timeout timeout))))
81+
([tasks timeout cancellation-token] (Task/WaitAll ^Task/1 (into-array Task tasks) (int (convert-timeout timeout)) cancellation-token)))
8382

8483

8584
(defn wait-any
@@ -96,12 +95,12 @@
9695
(nth tasks idx)))
9796
([tasks timeout]
9897
(let [^Task/1 task-array (into-array Task tasks)
99-
idx (Task/WaitAny task-array ^int (convert-timeout timeout))]
98+
idx (Task/WaitAny task-array (int (convert-timeout timeout)))]
10099
(when-not (= idx -1)
101100
(nth tasks idx))))
102101
([tasks timeout cancellation-token]
103102
(let [^Task/1 task-array (into-array Task tasks)
104-
idx (Task/WaitAny task-array (convert-timeout timeout) cancellation-token)]
103+
idx (Task/WaitAny task-array (int (convert-timeout timeout)) cancellation-token)]
105104
(when-not (= idx -1)
106105
(nth tasks idx)))))
107106

@@ -120,12 +119,12 @@
120119
(map result tasks)))
121120
([tasks timeout]
122121
(let [^Task/1 task-array (into-array Task tasks)]
123-
(Task/WaitAll task-array ^int (convert-timeout timeout))
124-
(map result tasks)))
122+
(when (Task/WaitAll task-array (int (convert-timeout timeout)))
123+
(map result tasks))))
125124
([tasks timeout cancellation-token]
126125
(let [^Task/1 task-array (into-array Task tasks)]
127-
(Task/WaitAll task-array (convert-timeout timeout) cancellation-token)
128-
(map result tasks))))
126+
(when (Task/WaitAll task-array (int (convert-timeout timeout)) cancellation-token)
127+
(map result tasks)))))
129128

130129
(defn wait-any-result
131130
"Waits for any of the provided Task to complete execution. Returns the result of the task that completed or nil if a timeout occurred.
@@ -142,12 +141,12 @@
142141
(result (nth tasks idx)))))
143142
([tasks timeout]
144143
(let [^Task/1 task-array (into-array Task tasks)
145-
idx (Task/WaitAny task-array ^int (convert-timeout timeout))]
144+
idx (Task/WaitAny task-array (int (convert-timeout timeout)))]
146145
(when-not (= idx -1)
147146
(result (nth tasks idx)))))
148147
([tasks timeout cancellation-token]
149148
(let [^Task/1 task-array (into-array Task tasks)
150-
idx (Task/WaitAny task-array (convert-timeout timeout) cancellation-token)]
149+
idx (Task/WaitAny task-array (int (convert-timeout timeout)) cancellation-token)]
151150
(when-not (= idx -1)
152151
(result (nth tasks idx))))))
153152

@@ -160,20 +159,20 @@
160159
161160
Usage:
162161
(t/await (t/delay-task 1000))"
163-
[milliseconds]
162+
^Task [milliseconds]
164163
(Task/Delay (int milliseconds)))
165164

166165
(defn ->task
167166
"Wraps a value in a completed Task<object>.
168167
169168
Usage:
170169
(t/->task 42) ;=> completed Task whose result is 42"
171-
[value]
170+
^TaskObj [value]
172171
(Task/FromResult (type-args Object) value))
173172

174173
(defn completed-task
175174
"Returns a cached, already-completed void Task."
176-
[]
175+
^Task []
177176
Task/CompletedTask)
178177

179178
(defn task?
@@ -186,6 +185,6 @@
186185
187186
Usage:
188187
(t/await (t/run (fn [] (+ 1 2 3))))"
189-
[f]
188+
^TaskObj [f]
190189
(let [func (gen-delegate |System.Func`1[System.Object]| [] (f))]
191190
(Task/Run func)))

Clojure/Clojure/CljCompiler/Ast/AwaitExpr.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,38 +89,40 @@ public Expr Parse(ParserContext pcon, object frm)
8989
pcon.SetRhc(RHC.Expression).SetAssign(false),
9090
RT.second(form));
9191

92-
Type taskType;
93-
if (taskExpr.HasClrType)
94-
{
95-
taskType = taskExpr.ClrType;
96-
97-
bool isTaskType =
98-
taskType == typeof(Task)
99-
|| taskType == typeof(ValueTask)
100-
|| (taskType.IsGenericType &&
101-
(taskType.GetGenericTypeDefinition() == typeof(Task<>)
102-
|| taskType.GetGenericTypeDefinition() == typeof(ValueTask<>)));
103-
104-
if (!isTaskType)
105-
{
106-
if (taskType == typeof(object))
107-
taskType = typeof(Task<object>);
108-
else
109-
throw new ParseException(
110-
$"(await* ...) requires a Task, Task<T>, ValueTask, or ValueTask<T>, got: {taskType.FullName}");
111-
}
112-
}
113-
else
114-
{
115-
taskType = typeof(Task<object>);
116-
}
92+
//Type taskType;
93+
//if (taskExpr.HasClrType)
94+
//{
95+
// taskType = taskExpr.ClrType;
96+
97+
// bool isTaskType =
98+
// taskType == typeof(Task)
99+
// || taskType == typeof(ValueTask)
100+
// || (taskType.IsGenericType &&
101+
// (taskType.GetGenericTypeDefinition() == typeof(Task<>)
102+
// || taskType.GetGenericTypeDefinition() == typeof(ValueTask<>)));
103+
104+
// if (!isTaskType)
105+
// {
106+
// if (taskType == typeof(object))
107+
// taskType = typeof(Task<object>);
108+
// else
109+
// throw new ParseException(
110+
// $"(await* ...) requires a Task, Task<T>, ValueTask, or ValueTask<T>, got: {taskType.FullName}");
111+
// }
112+
//}
113+
//else
114+
//{
115+
// taskType = typeof(Task<object>);
116+
//}
117+
118+
Type taskType = taskExpr.HasClrType ? taskExpr.ClrType : null;
117119

118120
MethodInfo awaitMethod =
119121
Compiler.AsyncMethodCache.ResolveAwaitMethod(taskType, out Type resultType);
120122

121123
if (awaitMethod is null)
122124
throw new ParseException(
123-
"Failed to resolve AsyncHelpers.Await method for type: " + taskType.FullName);
125+
$"(await* ...) requires a Task, Task<T>, ValueTask, or ValueTask<T>, got: {taskType.FullName}");
124126

125127
//method.HasAwait = true;
126128

0 commit comments

Comments
 (0)