This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
+36
@@ -0,0 +1,36 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
# RUN: %parse-mojo-isolated %s -mlir-print-debuginfo | kgen-opt -lower-semantic-cf -check-lifetimes -verify-parameters -verify-diagnostics
|
||||
|
||||
|
||||
def takeIt[T: def () -> None, //](state: T):
|
||||
state()
|
||||
|
||||
struct MoveMe(Movable):
|
||||
var x:Int
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.x = move.x
|
||||
def __deinit__(deinit self:Self):
|
||||
pass
|
||||
|
||||
def use(d:MoveMe):
|
||||
pass
|
||||
|
||||
# CHECK-LABEL: lit.fn @"toy
|
||||
def toy(var byMove: MoveMe): # expected-note {{'byMove' declared here}}
|
||||
def myclosure() {var byMove^}:
|
||||
use(byMove)
|
||||
|
||||
use(byMove) # expected-error {{use of uninitialized value 'byMove'}}
|
||||
takeIt(myclosure)
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# RUN: %parse-mojo-isolated -verify-diagnostics %s
|
||||
|
||||
# A closure cannot derive an interior origin from a captured container while
|
||||
# an interior reference to the same container is also captured.
|
||||
# TODO: lift this limitation
|
||||
|
||||
struct MyList[T: AnyType](Movable where False):
|
||||
var data: UnsafePointer[Self.T, UntrackedOrigin[mut=True]]
|
||||
|
||||
def __init__(out self):
|
||||
self.data = UnsafePointer[
|
||||
Self.T, UntrackedOrigin[mut=True]].unsafe_dangling()
|
||||
|
||||
def __deinit__(deinit self):
|
||||
pass
|
||||
|
||||
@__unsafe_nested_origins_read_only
|
||||
def __getitem__(
|
||||
ref self,
|
||||
) -> ref[self.data._get_ref_with_unsafe_interior_origin["element"](self)] Self.T:
|
||||
return self.data._get_ref_with_unsafe_interior_origin["element"](self)
|
||||
|
||||
|
||||
struct MyPtr[origin: Origin[mut=False]]:
|
||||
var p: UnsafePointer[Int, Self.origin]
|
||||
|
||||
def __init__(out self, *, ref [Self.origin] to: Int):
|
||||
self.p = UnsafePointer[Int, Self.origin].address_of(to)
|
||||
|
||||
def get(self) -> Int:
|
||||
return self.p[]
|
||||
|
||||
|
||||
def test_derive_and_capture_interior[O: Origin[mut=True]](ref [O] list: MyList[Int]):
|
||||
var p = MyPtr(to=list[])
|
||||
|
||||
@always_inline
|
||||
def inner() {imm}:
|
||||
# expected-error @below {{cannot derive an interior origin from a captured container while an interior reference to the container is also captured}}
|
||||
_ = list[]
|
||||
_ = p.get()
|
||||
|
||||
inner()
|
||||
|
||||
|
||||
def main():
|
||||
var list = MyList[Int]()
|
||||
test_derive_and_capture_interior(list)
|
||||
mojo/corpus/diagnostics/files/mojo-parser/closures/closure_marker_conformance_refinement_errors.mojo
Generated
+71
@@ -0,0 +1,71 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# RUN: %parse-mojo-isolated -verify-diagnostics %s
|
||||
|
||||
# COM: Negative companion to closure_wrapper_marker_conformances.mojo
|
||||
# COM: (MOCO-4240). Marker-trait conformance on closure wrappers must stay
|
||||
# COM: gated on the closure's actual properties: a memory-convention closure
|
||||
# COM: is not RegisterPassable, and a closure move-capturing a non-copyable
|
||||
# COM: value is not ImplicitlyCopyable.
|
||||
|
||||
|
||||
# A plain (non-register-passable) struct: capturing it forces the closure to
|
||||
# the memory convention.
|
||||
struct MemOnly(Copyable, ImplicitlyCopyable, Movable, Deinitable):
|
||||
var x: Int
|
||||
|
||||
def __init__(out self):
|
||||
self.x = 0
|
||||
|
||||
|
||||
struct MoveOnly(Movable, Deinitable):
|
||||
var x: Int
|
||||
|
||||
def __init__(out self):
|
||||
self.x = 0
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def needs_rp[
|
||||
FuncType: def () -> Int,
|
||||
# expected-note @below {{constraint declared here evaluated to False, expected 'conforms_to(FuncType, RegisterPassable)'}}
|
||||
](func: FuncType) -> Int where conforms_to(FuncType, RegisterPassable):
|
||||
return func()
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def needs_ic[
|
||||
FuncType: def () -> Int,
|
||||
# expected-note @below {{constraint declared here evaluated to False, expected 'conforms_to(FuncType, ImplicitlyCopyable)'}}
|
||||
](func: FuncType) -> Int where conforms_to(FuncType, ImplicitlyCopyable):
|
||||
return func()
|
||||
|
||||
|
||||
def use_mem() -> Int:
|
||||
var m = MemOnly()
|
||||
|
||||
def mem_closure() {var m} -> Int:
|
||||
return m.x
|
||||
|
||||
# expected-error @below {{invalid call to 'needs_rp': violated constraint}}
|
||||
return needs_rp(mem_closure)
|
||||
|
||||
|
||||
def use_move() -> Int:
|
||||
var m = MoveOnly()
|
||||
|
||||
def move_closure() {var m^} -> Int:
|
||||
return m.x
|
||||
|
||||
# expected-error @below {{invalid call to 'needs_ic': violated constraint}}
|
||||
return needs_ic(move_closure)
|
||||
@@ -0,0 +1,74 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# RUN: %parse-mojo-isolated -verify-diagnostics %s
|
||||
|
||||
|
||||
# expected-error @+1 {{the 'escaping' function effect is no longer supported}}
|
||||
def escaping_effect_is_rejected(closure: def() escaping -> None):
|
||||
pass
|
||||
|
||||
|
||||
def escaping_on_nested_decl_is_rejected():
|
||||
# expected-error @below {{the 'escaping' function effect is no longer supported}}
|
||||
def myclosure() escaping:
|
||||
pass
|
||||
|
||||
|
||||
# COM: https://github.com/modular/mojo/issues/1223
|
||||
# COM: When a runtime argument has incorrect type, nested function bodies may
|
||||
# COM: still be resolved. Ensure that we don't crash when the arg is used.
|
||||
struct Parametric[a: Int](Movable where False):
|
||||
pass
|
||||
|
||||
|
||||
def test_suppressed_dyn_binding_error[
|
||||
x: Int
|
||||
# expected-error @below {{parametric functions must not be used as arguments; pass as a parameter instead}}
|
||||
# expected-note @below {{alternatively, bind its type parameters to create a concrete function}}
|
||||
](pval: Parametric[x], func: def[y: Int](p: Parametric[y]) thin -> None):
|
||||
def nested():
|
||||
func(pval)
|
||||
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Closure type error message tests (MOCO-4052)
|
||||
# Verify that closure types print with readable signatures, not mangled names.
|
||||
# A capture is required throughout: an uncaptured nested `def` is a thin
|
||||
# function, not a closure.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
|
||||
def test_closure_to_int_error():
|
||||
"""Closure assigned to Int shows a readable function signature."""
|
||||
var x = 0
|
||||
|
||||
def my_closure(arg: Int) {x} -> Int:
|
||||
return arg
|
||||
|
||||
# expected-error @+1 {{cannot implicitly convert 'def(arg: Int) -> Int' value to 'Int'}}
|
||||
var a: Int = my_closure
|
||||
|
||||
|
||||
def test_different_signature_closures():
|
||||
"""Closures with different signatures show distinct types in error."""
|
||||
var x = 0
|
||||
|
||||
def closure_int(arg: Int) {x} -> Int:
|
||||
return arg
|
||||
|
||||
def closure_float(arg: Float64) {x} -> Float64:
|
||||
return arg
|
||||
|
||||
var c = closure_int
|
||||
# expected-error @+1 {{cannot implicitly convert 'def(arg: Float64) -> Float64' value to 'def(arg: Int) -> Int'}}
|
||||
c = closure_float
|
||||
@@ -0,0 +1,37 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
# RUN: %parse-mojo-isolated --verify-diagnostics %s
|
||||
|
||||
def closure_holder_1() -> Int:
|
||||
# expected-note @below {{nested function declared here}}
|
||||
def bar(n: Int) -> Int:
|
||||
if n == 0:
|
||||
return 0
|
||||
# expected-error @below {{recursive references to nested functions are not supported; define 'bar' at file scope}}
|
||||
return n + bar(n - 1)
|
||||
|
||||
return bar(10)
|
||||
|
||||
|
||||
def closure_holder_2() -> Int:
|
||||
# expected-note @below {{nested function declared here}}
|
||||
def foo(n: Int) -> Int:
|
||||
def bar() {imm n} -> Int:
|
||||
if n <= 0:
|
||||
return 0
|
||||
# expected-error @below {{recursive references to nested functions are not supported; define 'foo' at file scope}}
|
||||
return foo(n - 1)
|
||||
return bar()
|
||||
|
||||
return foo(10)
|
||||
Generated
+89
@@ -0,0 +1,89 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# RUN: %parse-mojo-isolated -verify-diagnostics %s
|
||||
|
||||
# Regression test: declaration-time validation of explicit closure trait
|
||||
# conformance (e.g. `struct Foo(def(Int) -> Int):`).
|
||||
|
||||
# -----
|
||||
# COM: Explicit conformance fully satisfied — no diagnostics.
|
||||
|
||||
|
||||
struct GoodCallable(def(Int) -> Int):
|
||||
def __call__(self, x: Int) capturing -> Int:
|
||||
return x
|
||||
|
||||
|
||||
# -----
|
||||
# COM: Missing __call__ is caught at struct declaration time.
|
||||
|
||||
|
||||
# expected-error @below {{'MissingCall' does not implement all requirements for 'def(Bool) -> Int'}}
|
||||
# expected-note @below {{required function '__call__' is not implemented}}
|
||||
struct MissingCall(def(Bool) -> Int):
|
||||
pass
|
||||
|
||||
|
||||
# -----
|
||||
# COM: Wrong return type is caught at struct declaration time.
|
||||
|
||||
|
||||
# expected-error @below {{'WrongReturnType' does not implement all requirements for 'def(String) -> Int'}}
|
||||
# expected-note @below {{no '__call__' candidates have type 'def(WrongReturnType, String) capturing thin -> Int'}}
|
||||
struct WrongReturnType(def(String) -> Int):
|
||||
# expected-note @below {{candidate declared here with type 'def(self: WrongReturnType, x: String) capturing thin -> Bool'}}
|
||||
def __call__(self, x: String) capturing -> Bool:
|
||||
return True
|
||||
|
||||
|
||||
# -----
|
||||
# COM: A struct that explicitly declares conformance can be passed to a
|
||||
# COM: `def(Int)` parameter and called (positive, end-to-end). `capturing` is
|
||||
# COM: not required on `__call__` to conform.
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def call_it[F: def(Int)](func: F):
|
||||
func(5)
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct ExplicitCallable(def(Int)):
|
||||
var value: Int
|
||||
|
||||
def __call__(self, arg: Int):
|
||||
_ = arg + self.value
|
||||
|
||||
|
||||
def test_explicit_conformance_end_to_end():
|
||||
var c = ExplicitCallable(10)
|
||||
call_it(c)
|
||||
|
||||
|
||||
# -----
|
||||
# COM: A struct with a compatible __call__ but no explicit `(def(Int))`
|
||||
# COM: declaration is rejected — implicit conformance is not allowed.
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct NoExplicitConformance(Movable where False):
|
||||
var value: Int
|
||||
|
||||
def __call__(self, arg: Int):
|
||||
_ = arg + self.value
|
||||
|
||||
|
||||
def test_implicit_conformance_rejected():
|
||||
var c = NoExplicitConformance(10)
|
||||
# expected-error @below {{invalid call to 'call_it': value passed to 'func' cannot be converted from 'NoExplicitConformance' to 'F', argument type 'NoExplicitConformance' does not conform to trait 'def(Int) -> None'}}
|
||||
call_it(c)
|
||||
Generated
+96
@@ -0,0 +1,96 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# RUN: %parse-mojo-isolated -verify-diagnostics %s
|
||||
|
||||
|
||||
def id_simd[w: Int](v: SIMD[.uint32, w]) -> SIMD[.uint32, w]:
|
||||
return v
|
||||
|
||||
|
||||
def int32_simd[w: Int](v: SIMD[.int32, w]) -> SIMD[.int32, w]:
|
||||
return v
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def take_closure_param[
|
||||
C: def[w: Int](v: SIMD[.uint32, w]) -> SIMD[.uint32, w]
|
||||
]():
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def take_closure_arg[
|
||||
F: def[w: Int](v: SIMD[.uint32, w]) -> SIMD[.uint32, w]
|
||||
](f: F):
|
||||
pass
|
||||
|
||||
|
||||
def test_function_to_wrapper_struct():
|
||||
# success: function-typed value -> concrete closure wrapper struct type.
|
||||
take_closure_arg(id_simd)
|
||||
|
||||
# error: function-typed value with incompatible signature.
|
||||
# expected-error @below {{invalid call to 'take_closure_arg'}}
|
||||
take_closure_arg(int32_simd)
|
||||
|
||||
|
||||
def test_wrapper_to_closure_param():
|
||||
def wrapped_ok[
|
||||
w: Int
|
||||
](v: SIMD[.uint32, w]) {var} -> SIMD[.uint32, w]:
|
||||
return v
|
||||
|
||||
def wrapped_bad[
|
||||
w: Int
|
||||
](v: SIMD[.int32, w]) {var} -> SIMD[.int32, w]:
|
||||
return v
|
||||
|
||||
# success: closure-wrapper value -> closure-typed parameter.
|
||||
take_closure_param[type_of(wrapped_ok)]()
|
||||
|
||||
# error: closure-wrapper value with incompatible signature.
|
||||
# expected-error @below {{invalid call to 'take_closure_param'}}
|
||||
take_closure_param[type_of(wrapped_bad)]()
|
||||
|
||||
|
||||
def test_wrapper_to_wrapper_struct():
|
||||
def wrapped_1[
|
||||
w: Int
|
||||
](v: SIMD[.uint32, w]) {var} -> SIMD[.uint32, w]:
|
||||
return v
|
||||
|
||||
def wrapped_2[
|
||||
w: Int
|
||||
](v: SIMD[.int32, w]) {var} -> SIMD[.int32, w]:
|
||||
return v
|
||||
|
||||
# success: closure-wrapper value -> same concrete wrapper struct type.
|
||||
take_closure_arg[type_of(wrapped_1)](wrapped_1)
|
||||
|
||||
# error: closure-wrapper value with incompatible signature.
|
||||
# expected-error @below {{invalid call to 'take_closure_arg'}}
|
||||
take_closure_arg[type_of(wrapped_1)](wrapped_2)
|
||||
|
||||
|
||||
def symbol_renamed[n: Int](x: SIMD[.uint32, n]) -> SIMD[.uint32, n]:
|
||||
return x
|
||||
|
||||
|
||||
def test_canonical_equiv_symbol_to_parameter_success():
|
||||
def wrapped_renamed[
|
||||
n: Int
|
||||
](x: SIMD[.uint32, n]) {var} -> SIMD[.uint32, n]:
|
||||
return x
|
||||
|
||||
# Success: closure-struct type -> parameter type implicit conversion
|
||||
take_closure_param[type_of(wrapped_renamed)]()
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Copyright (c) 2026, Modular Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License v2.0 with LLVM Exceptions:
|
||||
# https://llvm.org/LICENSE.txt
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# RUN: %parse-mojo-isolated -verify-diagnostics %s
|
||||
|
||||
from std.builtin.device_passable import DevicePassable, DeviceTypeEncoder
|
||||
|
||||
trait MyInterface:
|
||||
def thing(self):
|
||||
...
|
||||
|
||||
|
||||
def make_closure(x: Int) -> Int:
|
||||
def parametric[T: MyInterface](a: T):
|
||||
# `X` is never referenced, so it's dead code the same as any other
|
||||
# unreferenced comptime alias: never resolved, no diagnostic.
|
||||
comptime X = A
|
||||
pass
|
||||
|
||||
return x
|
||||
|
||||
struct Mem(ImplicitlyCopyable):
|
||||
pass
|
||||
|
||||
def use(a:Mem):
|
||||
pass
|
||||
|
||||
# COM: ambiguous captures
|
||||
|
||||
def aThing(x: Int) -> Int:
|
||||
return x
|
||||
|
||||
|
||||
def aThing() -> Int:
|
||||
return 5
|
||||
|
||||
|
||||
def definesClosure():
|
||||
# expected-error @below {{ambiguous captured value: 'aThing'}}
|
||||
def aClosure() {var aThing}:
|
||||
pass
|
||||
|
||||
|
||||
struct Bar(ImplicitlyCopyable, RegisterPassable):
|
||||
var x: Int
|
||||
var y: Int
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
pass
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takeDevicePassable[T: DevicePassable](impl: T):
|
||||
pass
|
||||
|
||||
|
||||
def foo(bar: Bar) raises:
|
||||
# COM: This should fail because Bar is not trivial.
|
||||
|
||||
def closure(number: Int) {var bar} -> Int:
|
||||
return bar.x
|
||||
|
||||
# TODO: Rename Wrappers (MOCO-2541)
|
||||
# expected-error @below {{'takeDevicePassable' parameter 'T' has 'DevicePassable' type, but value has type 'def(number: Int) -> Int'}}
|
||||
takeDevicePassable[type_of(closure)](closure)
|
||||
|
||||
|
||||
# COM: Test that a closure capturing a non trivial
|
||||
# COM: type does NOT conform to TrivialRegisterPassable.
|
||||
# expected-note @below {{function declared here}}
|
||||
def takeTrivialRegisterPassable[T: TrivialRegisterPassable](impl: T):
|
||||
pass
|
||||
|
||||
|
||||
def testNonTrivialClosureNotTrivialRegisterPassable(bar: Bar) raises:
|
||||
def closure() {var bar} -> Int:
|
||||
return bar.x
|
||||
|
||||
# expected-error-re @below {{'{{.*}}' does not conform to trait 'TrivialRegisterPassable'}}
|
||||
takeTrivialRegisterPassable(closure)
|
||||
|
||||
|
||||
# COM: Test that a closure capturing a memory-only type does not conform to
|
||||
# COM: RegisterPassable.
|
||||
# expected-note @below {{function declared here}}
|
||||
def takeRegisterPassable[T: RegisterPassable](impl: T):
|
||||
pass
|
||||
|
||||
|
||||
def testMemoryOnlyClosureNotRegisterPassable(mem: Mem):
|
||||
def closure(number: Int) {var mem} -> Int:
|
||||
_ = mem
|
||||
return number
|
||||
|
||||
# expected-error-re @below {{'{{.*}}' does not conform to trait 'RegisterPassable'}}
|
||||
takeRegisterPassable(closure)
|
||||
|
||||
|
||||
# COM: Test that using deprecated `register_passable` effect emits a warning.
|
||||
def testRegisterPassableEffectDeprecated():
|
||||
# expected-warning @below {{the 'register_passable' function effect is no longer supported; use trait constraints like 'RegisterPassable & def(...) -> ...' instead}}
|
||||
def closure(x: Int) register_passable -> Int:
|
||||
return x
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def changeIt(mut aString: String):
|
||||
pass
|
||||
|
||||
|
||||
def nestedCaptureAll(mut aString: String) raises:
|
||||
def aFinalThing(x:Int) {imm}:
|
||||
# expected-error @below {{invalid call to 'changeIt': value passed to mutable argument 'aString' must be mutable}}
|
||||
changeIt(aString)
|
||||
|
||||
def aChildThing(x:Int) {var}:
|
||||
changeIt(aString)
|
||||
|
||||
|
||||
|
||||
def topLevel(x: String) -> String:
|
||||
return x
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takesClosure[T: def(Int) -> Int](cb: T, x: Int) -> Int:
|
||||
return cb(x)
|
||||
|
||||
|
||||
def useTopLevelClosure():
|
||||
# expected-error @below {{invalid call to 'takesClosure': 'takesClosure' parameter 'T' has 'def(Int) -> Int' type, but value has type 'def topLevel(x: String) thin -> String'}}
|
||||
takesClosure[topLevel](topLevel, 1)
|
||||
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Closure type mismatch errors
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
trait Animal:
|
||||
def speak(self):
|
||||
...
|
||||
|
||||
|
||||
trait Mammal(Animal):
|
||||
pass
|
||||
|
||||
struct Dog(Mammal, Movable where False):
|
||||
def speak(self):
|
||||
pass
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def takeClosureMammalParam[W: Mammal, C: def (x: W) -> None](impl: C):
|
||||
pass
|
||||
|
||||
|
||||
def traitConstraintMismatch[Q: Animal]():
|
||||
def closure(x: Q) {var}:
|
||||
x.speak()
|
||||
|
||||
# expected-error @below {{does not conform to trait 'def(x: W) -> None'}}
|
||||
takeClosureMammalParam(closure)
|
||||
|
||||
def closureWrongConvention(mut x: Dog) {var}:
|
||||
x.speak()
|
||||
|
||||
# expected-error @below {{'takeClosureMammalParam' parameter 'C' has 'def(x: Dog) -> None' type, but value has type 'def(mut x: Dog) -> None'}}
|
||||
takeClosureMammalParam[Dog, type_of(closureWrongConvention)](closureWrongConvention)
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Enforce Parameter Capture
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
trait Coord(ImplicitlyCopyable):
|
||||
pass
|
||||
|
||||
struct Cartesian(Coord):
|
||||
var x: Int
|
||||
var y: Int
|
||||
var z: Int
|
||||
|
||||
struct Sphere(Coord):
|
||||
var theta: Int
|
||||
var phi: Int
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def takeClosure[T: Coord, C:def() -> T](impl: C) -> T:
|
||||
_ = impl()
|
||||
|
||||
|
||||
def makeClosure[B:Int](something: Cartesian):
|
||||
def closureImpl() {var} -> Cartesian:
|
||||
return something
|
||||
# expected-error @below {{invalid call to 'takeClosure': 'takeClosure' parameter 'C' has 'def() -> Sphere' type, but value has type 'def() -> Cartesian'}}
|
||||
takeClosure[Sphere, type_of(closureImpl)](closureImpl)
|
||||
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Non-compatible parameter signatures disqualify conformance
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
def _print(x: Int):
|
||||
pass
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def callee_no_params[
|
||||
func: def() -> None,
|
||||
//,
|
||||
](closure: func):
|
||||
closure()
|
||||
|
||||
|
||||
def incompatible_param_signature() raises:
|
||||
var x = 42
|
||||
|
||||
@always_inline
|
||||
def my_func[param_only: Int]() {imm x}:
|
||||
_print(x)
|
||||
|
||||
# expected-error @below {{does not conform to trait 'def() -> None'}}
|
||||
callee_no_params(my_func)
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Multiple default capture conventions specified
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
|
||||
def multiple_default_capture_conventions(x: Int):
|
||||
# expected-error @below {{default capture convention was already specified; remove the duplicate}}
|
||||
# expected-note @below {{a capture convention (like 'mut' or 'var') before the capture list sets the default for all captured variables}}
|
||||
def my_closure(y: Int) {var, ref} -> Int:
|
||||
return y
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Incompatible capture conventions
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
|
||||
def incompatible_capture_conventions(x: Int):
|
||||
# expected-error @below {{'^' requires 'var' convention; write 'var x^' to move a capture}}
|
||||
def my_closure(y: Int) {ref x^} -> Int:
|
||||
return y
|
||||
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Default capture convention violation
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
def default_capture_convention_violation():
|
||||
var y = 20
|
||||
var x = 10
|
||||
|
||||
def my_fn() {imm, mut y}:
|
||||
# Assigning to `y` work
|
||||
y = 20
|
||||
# expected-error @below {{expression must be mutable in assignment}}
|
||||
x = 10
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# Capture RTP with no-read convention
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
def capture_RTP(x : Int) :
|
||||
# expected-error @below{{register passible value 'x' can not be captured by 'mut'. Do you mean 'imm'?}}
|
||||
def my_func() {mut x}:
|
||||
pass
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# By-copy / by-move unified closure captures still require a conforming type
|
||||
# when no scope assumption refines the generic bound (MOCO-4229 regression
|
||||
# guard: refinement must not swallow this diagnostic for a truly unbound T).
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
trait NotImplicitlyCopyable:
|
||||
pass
|
||||
|
||||
|
||||
# expected-error @below {{value of type 'T' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'}}
|
||||
def capture_by_copy_no_refinement[T: NotImplicitlyCopyable](z: T):
|
||||
# expected-error @below {{cannot capture z by copy because it is not copyable.}}
|
||||
def f() {var z}:
|
||||
_ = z
|
||||
|
||||
f()
|
||||
|
||||
|
||||
trait NotMovable:
|
||||
pass
|
||||
|
||||
|
||||
def capture_by_move_no_refinement[T: NotMovable](var z: T):
|
||||
# expected-error @below {{Cannot capture z by move because the type is not movable}}
|
||||
def h() {var z^}:
|
||||
_ = z
|
||||
|
||||
h()
|
||||
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# A closure capturing a DevicePassable value by reference is not DevicePassable
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# COM: A by-reference capture stores a host pointer (LIT::RefType) as its
|
||||
# COM: storage field, so the closure cannot be device-encoded even though the
|
||||
# COM: capture's type is itself DevicePassable (MOCO-4045). .
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct RegPassableDevice(
|
||||
DevicePassable, ImplicitlyCopyable, TrivialRegisterPassable
|
||||
):
|
||||
comptime device_type: AnyType = Int
|
||||
var value: Int
|
||||
|
||||
def _to_device_type(
|
||||
self, mut encoder: Some[DeviceTypeEncoder], target: MutOpaquePointer[_]
|
||||
):
|
||||
encoder.encode(self.value, target)
|
||||
|
||||
@staticmethod
|
||||
def get_type_name() -> String:
|
||||
return "RegPassableDevice"
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def takeDevicePassableByRef[T: DevicePassable](impl: T):
|
||||
pass
|
||||
|
||||
|
||||
def capture_device_passable_by_reference(value: RegPassableDevice) raises:
|
||||
def closure(argument: Int) {imm value} -> Int:
|
||||
return argument + value.value
|
||||
|
||||
# expected-error-re @below {{'{{.*}}' does not conform to trait 'DevicePassable'}}
|
||||
takeDevicePassableByRef(closure)
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct MemType:
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def apply(f: Some[RegisterPassable & def() -> None]):
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
var t = MemType()
|
||||
|
||||
def func() {var t^}:
|
||||
pass
|
||||
# expected-error @below {{invalid call to 'apply': value passed to 'f' cannot be converted from 'def() -> None' to 'T', argument type 'def() -> None' does not conform to trait 'def() -> None & RegisterPassable'}}
|
||||
apply(func)
|
||||
Reference in new issue
Block a user