Baseline: tree as shared
This commit is contained in:
1679 files changed
+1024562
No files matched your search
@@ -0,0 +1,648 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# 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 -verify-diagnostics
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takes_pos_only_arg(a: Int, b: Int, /):
|
||||
pass
|
||||
|
||||
|
||||
def test_pos_only_arg_passed_by_kw(x: Int):
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_only_arg': missing required argument: 'b'}}
|
||||
takes_pos_only_arg(x, b=x)
|
||||
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_only_arg': missing required argument: 'a'}}
|
||||
takes_pos_only_arg(b=x, a=x)
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takes_kw_only_arg(*, a: Int, b: Int, c: Int = 7):
|
||||
pass
|
||||
|
||||
|
||||
def test_missing_kw_only_arg(x: Int):
|
||||
# COM: missing kw-only error takes precedence over unknown keyword
|
||||
# expected-error @+1 {{invalid call to 'takes_kw_only_arg': missing required argument: 'b'}}
|
||||
takes_kw_only_arg(a=x, d=x)
|
||||
|
||||
# expected-error @+1 {{invalid call to 'takes_kw_only_arg': missing required argument: 'a'}}
|
||||
takes_kw_only_arg()
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takes_pos_or_kw_arg(i: Int, j: Int):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def var_arg_func(*args: Int):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{declared here}}
|
||||
def pack_func[*Ts: AnyType](*args: *Ts):
|
||||
pass
|
||||
|
||||
|
||||
def test_unknown_kw_arg(x: Int):
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_or_kw_arg': unexpected keyword argument 'c'}}
|
||||
takes_pos_or_kw_arg(x, c=x, j=x)
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_or_kw_arg': missing required argument: 'j'}}
|
||||
takes_pos_or_kw_arg(x, d=x, c=x)
|
||||
# expected-error @+1 {{invalid call to 'var_arg_func': unexpected keyword argument 'args'}}
|
||||
var_arg_func(args=x)
|
||||
# expected-error @+1 {{invalid call to 'pack_func': unexpected keyword argument 'args'}}
|
||||
pack_func(args=x)
|
||||
|
||||
|
||||
def test_passed_by_pos_and_kw_arg(x: Int):
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_or_kw_arg': missing required argument: 'j'}}
|
||||
takes_pos_or_kw_arg(x, i=x)
|
||||
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_or_kw_arg': unexpected keyword argument 'j'}}
|
||||
takes_pos_or_kw_arg(x, x, j=x, i=x)
|
||||
|
||||
|
||||
# expected-note @+1 {{declared here}}
|
||||
def takes_pos_or_kw_param[i: Int, j: Int]():
|
||||
pass
|
||||
|
||||
|
||||
def test_unknown_kw_param[x: Int]():
|
||||
# expected-error @+1 {{unexpected keyword parameter 'c'}}
|
||||
takes_pos_or_kw_param[x, c=x, j=x]
|
||||
# expected-error @+1 {{unexpected keyword parameter 'd'}}
|
||||
takes_pos_or_kw_param[x, d=x, c=x]
|
||||
# expected-error @below {{unexpected keyword parameter 'Ts'}}
|
||||
pack_func[Ts=Int]
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takes_pos_only_param[a: Int, b: Int, /]():
|
||||
pass
|
||||
|
||||
|
||||
def test_pos_only_param_passed_by_kw[x: Int]():
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_only_param': unexpected keyword parameter 'b'}}
|
||||
takes_pos_only_param[x, b=x]()
|
||||
|
||||
# expected-error @+1 {{invalid call to 'takes_pos_only_param': unexpected keyword parameter 'b'}}
|
||||
takes_pos_only_param[b=x, a=x]()
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takes_kw_only_param[*, a: Int, b: Int, c: Int = 7]():
|
||||
pass
|
||||
|
||||
|
||||
def test_missing_kw_only_param[x: Int]():
|
||||
# expected-error @+1 {{invalid call to 'takes_kw_only_param': unexpected keyword parameter 'd'}}
|
||||
takes_kw_only_param[a=x, d=x]()
|
||||
|
||||
# expected-error @+1 {{missing required keyword-only parameter: 'a'}}
|
||||
takes_kw_only_param[]()
|
||||
|
||||
|
||||
# expected-note @below {{declared here}}
|
||||
def missing_keyword_only_params_tricky[a: Int, /, *, b: Int, c: Int = 3]():
|
||||
pass
|
||||
|
||||
|
||||
def test_missing_keyword_only_params_tricky[x: Int]():
|
||||
# expected-error @below {{unexpected parameter}}
|
||||
missing_keyword_only_params_tricky[x, x, x]
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takes_kw_only_args(a: Int, b: Int, *args: Int, c: Int, d: Int = 2):
|
||||
pass
|
||||
|
||||
|
||||
def test_missing_positional_arg_with_vararg_keyword(x: Int):
|
||||
# expected-error @+1 {{invalid call to 'takes_kw_only_args': missing required argument: 'b'}}
|
||||
takes_kw_only_args(x, c=2)
|
||||
|
||||
|
||||
def test_missing_keyword_arg_with_vararg_keyword(x: Int):
|
||||
takes_kw_only_args(x, x, c=2)
|
||||
|
||||
|
||||
struct MemExample(ImplicitlyCopyable):
|
||||
def __init__(out self):
|
||||
pass
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
pass
|
||||
|
||||
|
||||
struct MemExampleTriviallyCopyable(ImplicitlyCopyable):
|
||||
def __init__(out self):
|
||||
pass
|
||||
|
||||
|
||||
def mutateMem(mut a: MemExample):
|
||||
pass
|
||||
|
||||
|
||||
def mutateMemTC(mut a: MemExampleTriviallyCopyable):
|
||||
pass
|
||||
|
||||
|
||||
def mutateInt(mut a: Int):
|
||||
pass
|
||||
|
||||
|
||||
def initialize_in_addrspace(
|
||||
memptr: UnsafePointer[
|
||||
MemExample, AnyOrigin[mut=True], address_space=AddressSpace(1)],
|
||||
regptr: UnsafePointer[
|
||||
Int, AnyOrigin[mut=True], address_space=AddressSpace(1)],
|
||||
):
|
||||
# expected-error @+1 {{value of type 'MemExample' cannot be copied or moved into a non-default address space}}
|
||||
memptr[] = MemExample()
|
||||
# ok
|
||||
regptr[] = Int()
|
||||
|
||||
|
||||
def mutate_in_addrspace(
|
||||
memptr: UnsafePointer[
|
||||
MemExample, AnyOrigin[mut=True], address_space=AddressSpace(1)],
|
||||
memtcptr: UnsafePointer[
|
||||
MemExampleTriviallyCopyable,
|
||||
AnyOrigin[mut=True],
|
||||
address_space=AddressSpace(1)],
|
||||
regptr: UnsafePointer[
|
||||
Int, AnyOrigin[mut=True], address_space=AddressSpace(1)],
|
||||
):
|
||||
# expected-error @+1 {{non-implicitly trivially copyable value cannot be copied from a non-default address space}}
|
||||
mutateMem(memptr[])
|
||||
# ok
|
||||
mutateMemTC(memtcptr[])
|
||||
# ok
|
||||
mutateInt(regptr[])
|
||||
|
||||
|
||||
def variadic_addr_space(
|
||||
memptr: UnsafePointer[
|
||||
MemExample, AnyOrigin[mut=True], address_space=AddressSpace(1)],
|
||||
regptr: UnsafePointer[
|
||||
Int, AnyOrigin[mut=True], address_space=AddressSpace(1)],
|
||||
):
|
||||
# expected-error @below {{non-implicitly trivially copyable value cannot be copied from a non-default address space}}
|
||||
pack_func(memptr[])
|
||||
# Ok.
|
||||
pack_func(regptr[])
|
||||
|
||||
|
||||
struct ParametricMutability(Movable where False):
|
||||
def take_inout(mut self): # expected-note {{function declared here}}
|
||||
# This is ok
|
||||
self.take_parametric()
|
||||
|
||||
def take_parametric(ref self):
|
||||
# expected-error @+1 {{invalid call to 'take_inout': invalid use of mutating method on rvalue of type 'ParametricMutability'}}
|
||||
self.take_inout()
|
||||
|
||||
|
||||
def test_ref[mut: Bool, //, origin: Origin[mut=mut]](ref[origin] arg: String):
|
||||
pass
|
||||
|
||||
|
||||
def call_test_ref(mut s: String):
|
||||
# expected-error @below {{cannot use parametric function as a runtime closure}}
|
||||
# expected-note @below {{parameter 'mut' of type 'Bool' is not bound}}
|
||||
var f1 = test_ref
|
||||
|
||||
# This is ok, because the only unbound parameter is the origin which is a
|
||||
# singleton type.
|
||||
var f2 = test_ref[mut=True, ...]
|
||||
# Can now bind the remaining parameter in a call.
|
||||
f2(s)
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct MyMutSpan[origin: Origin[mut=True]](Movable where False):
|
||||
pass
|
||||
|
||||
|
||||
def take_two_spans(a: MyMutSpan[_], b: MyMutSpan[_]):
|
||||
# This is totally fine, can take two different mutable spans.
|
||||
pass
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct MyStruct(ImplicitlyCopyable):
|
||||
var a: Int
|
||||
var b: Int
|
||||
|
||||
|
||||
def exclusivity[
|
||||
spanlife: Origin[mut=True]
|
||||
](mut x: MyStruct, span: MyMutSpan[spanlife]):
|
||||
# Compiler injects a temporary to make this ok.
|
||||
x = x
|
||||
|
||||
# Compiler injects a temporary to make this ok.
|
||||
x = x^
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'take_two_spans' call}}
|
||||
# expected-note @below {{'origin_of(spanlife)' memory accessed through reference embedded in value of type 'MyMutSpan[spanlife]'}}
|
||||
take_two_spans(span, span)
|
||||
|
||||
|
||||
def mutate_two[A: AnyType, B: AnyType](mut a: A, mut b: B):
|
||||
pass
|
||||
|
||||
|
||||
def take_two_owned[A: AnyType, B: AnyType](var a: A, var b: B):
|
||||
pass
|
||||
|
||||
|
||||
def mutate_one_read_one[A: AnyType, B: AnyType](mut a: A, b: B):
|
||||
pass
|
||||
|
||||
|
||||
def mutate_two_AnyLifetime(
|
||||
ref[AnyOrigin[mut=True]] a: Int, ref[AnyOrigin[mut=True]] b: Int
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def mutate_variadic_any[T: AnyType](mut *values: T):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def mutate_pack[*Ts: AnyType](mut *strs: *Ts):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def consume_owned_variadic_pack[*Ts: AnyType](var *inner: *Ts):
|
||||
pass
|
||||
|
||||
|
||||
def forward_borrowed_pack_to_mut_pack[*Ts: AnyType](*outer: *Ts):
|
||||
# expected-error @below {{cannot unpack a variadic pack into a call that requires a stricter mutability}}
|
||||
mutate_pack(*outer)
|
||||
|
||||
|
||||
def forward_unknown_mut_pack_to_mut_pack[
|
||||
*Ts: AnyType
|
||||
](outer: VariadicPack[origin=_, element_trait=AnyType, _, *Ts]):
|
||||
# expected-error @below {{cannot unpack a variadic pack into a call that requires a different ownership}}
|
||||
mutate_pack(*outer)
|
||||
|
||||
|
||||
def forward_borrowed_pack_to_owned_pack[*Ts: AnyType](*outer: *Ts):
|
||||
# expected-error @below {{cannot unpack a variadic pack into a call that requires a different ownership}}
|
||||
consume_owned_variadic_pack(*outer)
|
||||
|
||||
|
||||
def forward_unknown_ownership_pack[
|
||||
*Ts: AnyType, owned: Bool
|
||||
](outer: VariadicPack[origin=_, element_trait=AnyType, owned, *Ts]):
|
||||
# expected-error @below {{cannot unpack a variadic pack into a call that requires a different ownership}}
|
||||
consume_owned_variadic_pack(*outer)
|
||||
|
||||
|
||||
def inout_ref_exclusivity(mut a: Int, mut b: Int, mut s: MyStruct):
|
||||
# This is ok.
|
||||
mutate_two(a, b)
|
||||
|
||||
# This is not.
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two' call}}
|
||||
# expected-note @below {{'origin_of(a)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_two(a, a)
|
||||
|
||||
# This is ok: field sensitivity.
|
||||
mutate_two(s.a, s.b)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two' call}}
|
||||
# expected-note @below {{'origin_of(s.a)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_two(s.a, s.a)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two' call}}
|
||||
# expected-note @below {{'origin_of(s)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_two(s.a, s)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two' call}}
|
||||
# expected-note @below {{'origin_of(s.a)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_two(s, s.a)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'take_two_owned' call}}
|
||||
# expected-note @below {{'origin_of(s)' value is passed through aliasing 'var' argument}}
|
||||
take_two_owned(s^, s^)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed immutably to 'b' argument in 'mutate_one_read_one' call}}
|
||||
# expected-note @below {{'origin_of(s.a)' value is passed through aliasing 'imm' argument}}
|
||||
mutate_one_read_one(s, s.a)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two_AnyLifetime' call}}
|
||||
# expected-note @below {{'origin_of(a)' value is passed through aliasing 'ref' argument}}
|
||||
mutate_two_AnyLifetime(a, a)
|
||||
|
||||
# These are all ok.
|
||||
mutate_variadic_any[Int]()
|
||||
mutate_variadic_any(s)
|
||||
mutate_variadic_any(a, b)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'values' argument and passed mutably to 'values' argument in 'mutate_variadic_any' call}}
|
||||
# expected-note @below {{'origin_of(a)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_variadic_any(a, a)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'values' argument and passed mutably to 'values' argument in 'mutate_variadic_any' call}}
|
||||
# expected-note @below {{'origin_of(a)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_variadic_any(a, b, a)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'values' argument and passed mutably to 'values' argument in 'mutate_variadic_any' call}}
|
||||
# expected-note @below {{'origin_of(s)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_variadic_any(s, s)
|
||||
|
||||
# These are ok.
|
||||
mutate_pack(a)
|
||||
mutate_pack(a, s)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'strs' argument and passed mutably to 'strs' argument in 'mutate_pack' call}}
|
||||
# expected-note @below {{'origin_of(a)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_pack(a, a)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'strs' argument and passed mutably to 'strs' argument in 'mutate_pack' call}}
|
||||
# expected-note @below {{'origin_of(a)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_pack(a, b, a)
|
||||
|
||||
# expected-error @below {{aliasing values passed mutably to 'strs' argument and passed mutably to 'strs' argument in 'mutate_pack' call}}
|
||||
# expected-note @below {{'origin_of(s)' value is passed through aliasing 'mut' argument}}
|
||||
mutate_pack(s, s)
|
||||
|
||||
|
||||
def capture_exclusivity(var x: MemExample):
|
||||
@__parameter
|
||||
def capture_and_read(y: MemExample):
|
||||
_ = x^
|
||||
|
||||
# FIXME(MOCO-3241): Re-enable this.
|
||||
# xpected-error @below {{argument of call allows reading a memory location previously writable through implicit closure captures}}
|
||||
# xpected-note @below {{'origin_of(x)' value is passed through aliasing 'imm' argument}}
|
||||
capture_and_read(x)
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def param_inference_unrelated_error[T: AnyType](x: T, y: FloatLiteral[_]):
|
||||
pass
|
||||
|
||||
|
||||
def call_param_inference_unrelated_error():
|
||||
comptime x = "hello"
|
||||
comptime y = "world"
|
||||
# expected-error @below {{value passed to 'y' cannot be converted from 'StringLiteral["world"]' to 'FloatLiteral[y.value]', it depends on an unresolved parameter 'y.value'}}
|
||||
param_inference_unrelated_error(x, y)
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct MyRPStruct(RegisterPassable):
|
||||
var a: Int
|
||||
|
||||
def __deinit__(deinit self):
|
||||
pass
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct MyRPStruct2(RegisterPassable):
|
||||
var b: MyRPStruct
|
||||
|
||||
def __deinit__(deinit self):
|
||||
pass
|
||||
|
||||
|
||||
def take_owned_and_mutate_rp(var a: MyRPStruct2, mut b: MyRPStruct2):
|
||||
pass
|
||||
|
||||
|
||||
def rp_exclusivity(mut x: MyRPStruct2):
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'take_owned_and_mutate_rp' call}}
|
||||
# expected-note @below {{'origin_of(x)' value is passed through aliasing 'mut' argument}}
|
||||
take_owned_and_mutate_rp(x^, x)
|
||||
|
||||
|
||||
def take_and_mutate_rp(a: MyRPStruct, mut b: MyRPStruct2):
|
||||
pass
|
||||
|
||||
|
||||
def rp_exclusivity2(mut x: MyRPStruct2):
|
||||
# expected-error @below {{aliasing values passed immutably to 'a' argument and passed mutably to 'b' argument in 'take_and_mutate_rp' call}}
|
||||
# expected-note @below {{'origin_of(x)' value is passed through aliasing 'mut' argument}}
|
||||
take_and_mutate_rp(x.b, x)
|
||||
|
||||
def bad_interior_origin_example_exclusivity():
|
||||
var b : Buf
|
||||
# expected-error @below {{aliasing values passed immutably to 'size' argument and constructed as a result in 'Buf' initializer call}}
|
||||
# expected-note @below {{introduce a temporary to avoid mutating the call result while accessing it through an argument}}
|
||||
b = Buf(b.view()) # copy from own interior view, then reassign
|
||||
|
||||
struct Buf(Movable where False):
|
||||
var data: List[Int]
|
||||
|
||||
def view(self) -> ref[self.data[0]] Int:
|
||||
return self.data[0]
|
||||
def __init__(out self, ref size: Int):
|
||||
self.data = {}
|
||||
|
||||
|
||||
# MOCO-1242 - [QoI] Improve error message on trait failure for variadics (e.g. print with Formattable)
|
||||
# expected-note @below {{function declared here}}
|
||||
def my_print_variadic[*Ts: MyWritable](x: Int, *args: *Ts):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def my_print_single[T: MyWritable](value: T):
|
||||
pass
|
||||
|
||||
|
||||
def test_print_errors(s: MyStruct):
|
||||
# expected-error @below {{invalid call to 'my_print_variadic': an element of 'args' with type 'MyStruct' does not conform to trait 'MyWritable'; either prove the conformance with 'conforms_to', or add conformance}}
|
||||
my_print_variadic(1, s)
|
||||
|
||||
# expected-error @below {{invalid call to 'my_print_single': value passed to 'value' cannot be converted from 'MyStruct' to 'T', argument type 'MyStruct' does not conform to trait 'MyWritable'}}
|
||||
my_print_single(s)
|
||||
|
||||
|
||||
trait MyWritable:
|
||||
def method(self):
|
||||
pass
|
||||
|
||||
|
||||
# Issue #4499: https://github.com/modular/modular/issues/4499
|
||||
# Traits with ref self cause issues when used as parameter
|
||||
trait MyTrait4499:
|
||||
def method(ref self):
|
||||
...
|
||||
|
||||
|
||||
struct MyStruct4499(MyTrait4499, Movable where False):
|
||||
def method(ref self):
|
||||
pass
|
||||
|
||||
|
||||
struct Owner4499[T: MyTrait4499](Movable where False):
|
||||
def __init__(out self):
|
||||
pass
|
||||
|
||||
|
||||
def my_func4499(arg0: Owner4499, arg1: Owner4499):
|
||||
pass
|
||||
|
||||
|
||||
def test_4499_exclusivity():
|
||||
# Should be ok.
|
||||
my_func4499(Owner4499[MyStruct4499](), Owner4499[MyStruct4499]())
|
||||
|
||||
|
||||
# Test printing of apply expressions.
|
||||
def vararg_example(*args: Int, other: Int):
|
||||
pass
|
||||
|
||||
|
||||
def pack_example[*Ts: AnyType](*args: *Ts, other: Int):
|
||||
pass
|
||||
|
||||
|
||||
def generic_example[T: AnyType, //](a: T):
|
||||
pass
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct StructWithFlexParam[T: AnyType, //, x: T](Movable where False):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takeWith4(a: StructWithFlexParam[4]):
|
||||
pass
|
||||
|
||||
|
||||
def test_print_apply_expressions():
|
||||
# expected-note @below {{.T of the first type is '__MLIRType[None]' but the second type is 'Int'}}
|
||||
# expected-error @below {{invalid call to 'takeWith4': value passed to 'a' cannot be converted from 'StructWithFlexParam[vararg_example(Int(0), Int(1), Int(2), Int(3), Int(4), other=Int(5))]' to 'StructWithFlexParam[Int(4)]'}}
|
||||
takeWith4(StructWithFlexParam[vararg_example(0, 1, 2, 3, 4, other=5)]())
|
||||
# expected-note @below {{.T of the first type is '__MLIRType[None]' but the second type is 'Int'}}
|
||||
# expected-error @below {{invalid call to 'takeWith4': value passed to 'a' cannot be converted from 'StructWithFlexParam[pack_example[Int, String](Int(0), String("foo"), other=Int(5))]' to 'StructWithFlexParam[Int(4)]'}}
|
||||
takeWith4(StructWithFlexParam[pack_example(0, "foo", other=5)]())
|
||||
# expected-note @below {{.T of the first type is '__MLIRType[None]' but the second type is 'Int'}}
|
||||
# expected-error @below {{from 'StructWithFlexParam[generic_example(Int(0))]' to}}
|
||||
takeWith4(StructWithFlexParam[generic_example(0)]())
|
||||
# expected-note @below {{.T of the first type is '__MLIRType[None]' but the second type is 'Int'}}
|
||||
# expected-error @below {{invalid call to 'takeWith4': value passed to 'a' cannot be converted from 'StructWithFlexParam[vararg_example(other=Int(5))]' to 'StructWithFlexParam[Int(4)]'}}
|
||||
takeWith4(StructWithFlexParam[vararg_example(other=5)]())
|
||||
|
||||
|
||||
struct DifferExample[shape: Int, addr: Int](Movable where False):
|
||||
pass
|
||||
|
||||
|
||||
def differ_take[ # expected-note {{function declared here}}
|
||||
tile_impl: def[input_addr: Int, output_shape: Int, output_addr: Int](
|
||||
DifferExample[output_shape, output_addr],
|
||||
) thin -> None,
|
||||
]():
|
||||
pass
|
||||
|
||||
|
||||
def differ_wrong[
|
||||
input_addr: Int, output_shape: Int, output_addr: Int
|
||||
](output: DifferExample[output_shape, input_addr]):
|
||||
# expected-error @below {{has 'def[input_addr: Int, output_shape: Int, output_addr: Int](DifferExample[output_shape, output_addr]) thin -> None' type, but value has type 'def differ_wrong[input_addr: Int, output_shape: Int, output_addr: Int](output: DifferExample[output_shape, input_addr]) thin -> None'}}
|
||||
# expected-note @below {{.output.addr of the first value is 'output_addr' but the second value is 'input_addr'}}
|
||||
differ_take[differ_wrong]()
|
||||
|
||||
|
||||
# expected-note @below {{cannot be converted from 'StringLiteral[""]' to 'Int'}}
|
||||
def call_error_location(a: Int):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @below {{cannot be converted from 'StringLiteral[""]' to 'IntLiteral}}
|
||||
def call_error_location(a: IntLiteral):
|
||||
pass
|
||||
|
||||
|
||||
def test_call_error_location():
|
||||
# The error location should be on the operand, not the call.
|
||||
call_error_location(
|
||||
# expected-error @below {{no matching function in call to 'call_error_location'}}
|
||||
""
|
||||
)
|
||||
|
||||
|
||||
@__unsafe_nested_origins_read_only
|
||||
def nested_mutability_disabled1(ref a: String, b: String): pass
|
||||
@__unsafe_nested_origins_read_only
|
||||
def nested_mutability_disabled2(mut a: String, b: String): pass
|
||||
|
||||
def test_nested_mutability_disabled():
|
||||
var s : String
|
||||
nested_mutability_disabled1(s, s) # This is ok, 'ref' is treated readonly
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed immutably to 'b' argument in 'nested_mutability_disabled2' call}}
|
||||
# expected-note @below {{'origin_of(s)' value is passed through aliasing 'imm' argument}}
|
||||
nested_mutability_disabled2(s, s)
|
||||
|
||||
# Subtree origin exclusivity.
|
||||
#
|
||||
# A subtree origin (x~) may refer to x or any field/interior under x, so it
|
||||
# conflicts with those accesses whenever either side is mutable. Distinct
|
||||
# sibling roots remain fine (field sensitivity of the subtree root).
|
||||
|
||||
# Coerce a ref to a subtree view rooted at `subtree_root`.
|
||||
@__unsafe_nested_origins_read_only
|
||||
def get_subtree_ref[T: AnyType, //, subtree_root: Origin](
|
||||
ref [subtree_root.subtree] x: T
|
||||
) -> ref[subtree_root.subtree] T:
|
||||
return x
|
||||
|
||||
|
||||
def test_subtree_origin_exclusivity(mut s: MyStruct, mut a: Int, mut b: Int):
|
||||
# Sibling fields: still fine.
|
||||
mutate_two(s.a, s.b)
|
||||
|
||||
# Independent locals through subtree views: fine.
|
||||
mutate_two(
|
||||
get_subtree_ref[origin_of(a)](a), get_subtree_ref[origin_of(b)](b)
|
||||
)
|
||||
|
||||
# Subtree of one field vs a sibling field: fine (roots do not overlap).
|
||||
mutate_two(get_subtree_ref[origin_of(s.a)](s.a), s.b)
|
||||
|
||||
# Whole-object subtree vs a field under it: conflict (s~ may be s.b).
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two' call}}
|
||||
# expected-note @below {{'origin_of(s.b)' value is passed through aliasing 'mut' argument 'b'}}
|
||||
mutate_two(get_subtree_ref[origin_of(s)](s.a), s.b)
|
||||
|
||||
# Two subtree views of the same owner: conflict.
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two' call}}
|
||||
# expected-note @below {{'origin_of(origin_of(s).subtree)' value is passed through aliasing 'mut' argument 'b'}}
|
||||
mutate_two(
|
||||
get_subtree_ref[origin_of(s)](s.a), get_subtree_ref[origin_of(s)](s.b)
|
||||
)
|
||||
|
||||
# Subtree of a field vs that same field: conflict.
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed mutably to 'b' argument in 'mutate_two' call}}
|
||||
# expected-note @below {{'origin_of(s.a)' value is passed through aliasing 'mut' argument 'b'}}
|
||||
mutate_two(get_subtree_ref[origin_of(s.a)](s.a), s.a)
|
||||
|
||||
# Mutable subtree view + immutable overlapping access: conflict.
|
||||
# expected-error @below {{aliasing values passed mutably to 'a' argument and passed immutably to 'b' argument in 'mutate_one_read_one' call}}
|
||||
# expected-note @below {{'origin_of(s.a)' value is passed through aliasing 'imm' argument 'b'}}
|
||||
mutate_one_read_one(get_subtree_ref[origin_of(s)](s.a), s.a)
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,630 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# 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 -verify-diagnostics | FileCheck %s
|
||||
|
||||
|
||||
def marker():
|
||||
pass
|
||||
|
||||
struct Unmovable(Movable where False):
|
||||
def __init__(out self):
|
||||
pass
|
||||
|
||||
|
||||
def throwing_fn() raises -> Int:
|
||||
return 0
|
||||
|
||||
|
||||
def literal_promotion[cond: Bool]():
|
||||
# This needs to coerce to the materialization type of float literal
|
||||
comptime a = 2.0 if cond else Int(3)
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# Assignment operator
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
struct ListInitializable[T: AnyType](ImplicitlyCopyable):
|
||||
def __init__(
|
||||
out self, *elements: Self.T, __list_literal__: NoneType = None
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
struct RHSInferenceStruct(Movable where False):
|
||||
var field: ListInitializable[Int]
|
||||
|
||||
def __getitem__(self) -> ListInitializable[Int]:
|
||||
pass
|
||||
|
||||
def __setitem__(self, value: ListInitializable[Int]):
|
||||
pass
|
||||
|
||||
|
||||
# None of these should be ambiguous.
|
||||
def test_rhs_inference():
|
||||
var a: ListInitializable[Int]
|
||||
a = [] # DeclRedefode
|
||||
(a) = [] # ParenNode
|
||||
|
||||
var lf: RHSInferenceStruct
|
||||
(lf).field = [] # AttributeRedefode
|
||||
|
||||
lf[] = [] # SubscriptNode
|
||||
|
||||
a, lf.field = [], [] # TupleNode
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_var_decl_patterns
|
||||
def test_var_decl_patterns(c: Bool) raises:
|
||||
# CHECK-NEXT: lit.call {{.*}}marker
|
||||
marker()
|
||||
|
||||
# Var patterns in a def are emitted inline, not at top of function.
|
||||
|
||||
# CHECK-NEXT: %x = lit.var.decl "x"
|
||||
# CHECK-NEXT: [[VAL:%.*]] = kgen.param.constant: !alias_Int1 = <rebind(:!Int {:scalar<index> 42})>
|
||||
# CHECK-NEXT: lit.ref.store [[VAL]], %x
|
||||
(var x) = 42
|
||||
|
||||
# This var inside the cond is scoped correctly even though we're in a def.
|
||||
# CHECK: lit.call {{.*}}marker
|
||||
marker()
|
||||
|
||||
# CHECK: hlcf.elif %{{.*}} {
|
||||
# CHECK-NEXT: [[X2:%.*]] = lit.var.decl "x"
|
||||
# CHECK-NEXT: [[VAL:%.*]] = kgen.param.constant: !alias_Int1 = <rebind(:!Int {:scalar<index> 42})>
|
||||
# CHECK-NEXT: lit.ref.store [[VAL]], [[X2]]
|
||||
if c:
|
||||
(var x) = 42
|
||||
|
||||
# This must load X to add to it.
|
||||
# CHECK: kgen.rebind %x
|
||||
# CHECK: lit.call {{.*}}SIMD::@"__add__{{.*}}
|
||||
(var _) = x + 1
|
||||
|
||||
# This should not load X.
|
||||
# CHECK-NOT: lit.ref.load %x
|
||||
(var _) = x
|
||||
|
||||
var lf: RHSInferenceStruct
|
||||
# expected-warning @+1 {{'var' pattern didn't declare a new variable, it can be removed}}
|
||||
(var lf.field) = []
|
||||
|
||||
# CHECK: lit.call {{.*}}marker
|
||||
marker()
|
||||
|
||||
# CHECK that the var pattern covers both tup1 and tup2
|
||||
# CHECK: lit.var.decl "tup1" var
|
||||
# CHECK-NEXT: lit.var.decl "tup2" var
|
||||
(var tup1, var tup2) = 1, 2
|
||||
|
||||
# Check that trailing commas work.
|
||||
# https://github.com/modular/modular/issues/1649
|
||||
x, x = 1, 2 # worked
|
||||
x, x, = 1, 2 # failed
|
||||
|
||||
# Verify that we stop parsing at the end of the statement - we shouldn't parse
|
||||
# the "fn" as part of the list (a function expression).
|
||||
_ = (1,)
|
||||
|
||||
def test() capturing:
|
||||
pass
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_ref_decl_patterns
|
||||
def test_ref_decl_patterns(a: List[Int], mut b: List[Int]):
|
||||
# CHECK-NEXT: [[ZERO:%.*]] = kgen.param.constant: !Int = <{:scalar<index> 0}>
|
||||
# CHECK-NEXT: [[ASUB:%.*]] = lit.call {{.*}}List::@"__getitem__{{.*}}(%a, [[ZERO]])
|
||||
# CHECK-NEXT: %r = lit.var.decl "r" ref
|
||||
# CHECK-NEXT: lit.ref.store [[ASUB]], %r
|
||||
ref r = a[0]
|
||||
|
||||
# CHECK-NEXT: [[ZERO:%.*]] = kgen.param.constant: !Int = <{:scalar<index> 0}>
|
||||
# CHECK-NEXT: [[BSUB:%.*]] = lit.call {{.*}}List::@"__getitem__{{.*}}(%b, [[ZERO]])
|
||||
# CHECK-NEXT: %r2 = lit.var.decl "r2" ref
|
||||
# CHECK-NEXT: lit.ref.store [[BSUB]], %r2
|
||||
ref r2 = b[0]
|
||||
|
||||
# CHECK-NEXT: [[RREF:%.*]] = lit.ref.load %r
|
||||
# CHECK-NEXT: %r3 = lit.var.decl "r3" ref
|
||||
# CHECK-NEXT: lit.ref.store [[RREF]], %r3
|
||||
ref r3 = r
|
||||
|
||||
# CHECK-NEXT: [[R2REF:%.*]] = lit.ref.load %r2
|
||||
# CHECK-NEXT: [[RREF:%.*]] = lit.ref.load %r
|
||||
# CHECK-NEXT: [[RVAL:%.*]] = lit.ref.load [[RREF]]
|
||||
# CHECK-NEXT: lit.call {{.*}}SIMD::@"__iadd__{{.*}}([[R2REF]], [[RVAL]])
|
||||
r2 += r
|
||||
|
||||
# CHECK-NEXT: [[R2REF:%.*]] = lit.ref.load %r2
|
||||
# CHECK-NEXT: [[R3REF:%.*]] = lit.ref.load %r3
|
||||
# CHECK-NEXT: [[R3VAL:%.*]] = lit.ref.load [[R3REF]]
|
||||
# CHECK-NEXT: lit.call {{.*}}SIMD::@"__iadd__{{.*}}([[R2REF]], [[R3VAL]])
|
||||
r2 += r3
|
||||
|
||||
# CHECK-NEXT: [[R2VAL:%.*]] = lit.ref.load %r2
|
||||
# CHECK-NEXT: %r4 = lit.var.decl "r4" ref
|
||||
# CHECK-NEXT: lit.ref.store [[R2VAL]], %r4
|
||||
ref r4 = r2
|
||||
|
||||
# CHECK-NEXT: [[R4REF:%.*]] = lit.ref.load %r4
|
||||
# CHECK-NEXT: [[ONE:%.*]] = kgen.param.constant: !Int = <{:scalar<index> 1}>
|
||||
# CHECK-NEXT: lit.call {{.*}}SIMD::@"__iadd__{{.*}}([[R4REF]], [[ONE]])
|
||||
r4 += 1
|
||||
|
||||
# Not useful, but this should work.
|
||||
# CHECK-NEXT: %r5 = lit.var.decl "r5" ref
|
||||
ref r5: Int
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_type_patterns
|
||||
def test_type_patterns():
|
||||
# CHECK-NEXT: lit.call {{.*}}marker
|
||||
marker()
|
||||
|
||||
# CHECK: %a = lit.var.decl "a" var : !lit.ref<:meta<!Int> #alias_Int,
|
||||
(var a): Int
|
||||
|
||||
# CHECK: %b = lit.var.decl "b" var : !lit.ref<!UInt8,
|
||||
# CHECK-NEXT: [[TMP:%.*]] = kgen.param.constant: {{.*}}int_literal 4>
|
||||
# CHECK-NEXT: [[TMP2:%.*]] = lit.call {{.*}}UInt8::@"__init__{{.*}}([[TMP]])
|
||||
# CHECK-NEXT: lit.ref.store [[TMP2]], %b
|
||||
var b: UInt8 = 4
|
||||
|
||||
# Show that the type annotation allows us to use the type in the pattern to
|
||||
# infer the RHS type of the collection.
|
||||
# CHECK: %c = lit.var.decl "c" var : !lit.ref<!lit.struct<#List <:!AnyType_Copyable_Movable !Int>>,
|
||||
# CHECK: lit.call {{.*}}List::@"__init__
|
||||
var c: List[Int] = []
|
||||
|
||||
# declare multiple variables at once.
|
||||
# CHECK: %d = lit.var.decl "d" var : !lit.ref<!Int,
|
||||
# CHECK: %e = lit.var.decl "e" var : !lit.ref<!Int,
|
||||
var (d, e): Tuple[Int, Int]
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# Test return slot optimization
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
# NOTE: Don't remove this argument, this was defeating return slot opzn.
|
||||
def getUnmovable(a: Unmovable) -> Unmovable:
|
||||
return Unmovable()
|
||||
|
||||
|
||||
# This can only be codegen'd directly into x.
|
||||
# CHECK-LABEL: lit.fn @"testUnmovable
|
||||
def testUnmovable(a: Unmovable):
|
||||
# CHECK-NEXT: %x = lit.var.decl "x"
|
||||
# CHECK-NEXT: lit.call {{.*}}(%a, %x)
|
||||
var x: Unmovable = getUnmovable(a)
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# type_of
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
comptime _index = __mlir_type.index
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"simple_typeof_return(index)"(%x: index) -> index
|
||||
def simple_typeof_return(x: _index) -> type_of(x):
|
||||
return x
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"typeof_arg(index,index)"(%x: index, %y: index) -> index
|
||||
def typeof_arg(x: __mlir_type.index, y: type_of(x)) -> _index:
|
||||
var z: type_of(x) = y
|
||||
return z
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"typeof_dynval_in_param(
|
||||
def typeof_dynval_in_param(x: _index):
|
||||
# CHECK-NEXT: %y = lit.var.decl
|
||||
# CHECK-NEXT: lit.call {{.*}}String::@"__init__
|
||||
var y = String()
|
||||
|
||||
# CHECK-NEXT: lit.alias.decl *"a`1": non_struct_type = <index>
|
||||
comptime a = type_of(x)
|
||||
# CHECK-NEXT: lit.alias.decl *"b`2": meta<!Int> = <#alias_Int>
|
||||
comptime b = type_of(y.__len__())
|
||||
|
||||
# CHECK-NEXT: lit.alias.decl *"c`3": meta<!Int> = <#alias_Int>
|
||||
comptime c = type_of(throwing_fn())
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# origin_of
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"lifetime_of
|
||||
def lifetime_of(x: Unmovable, y: Unmovable, mut z: Unmovable):
|
||||
# CHECK-NEXT: lit.alias.decl *"lt0{{.*}}:origin<false> {}
|
||||
comptime lt0 = origin_of()
|
||||
# CHECK-NEXT: lit.alias.decl *"lt1{{.*}}:origin<false> *"x`">>
|
||||
comptime lt1 = origin_of(x)
|
||||
# CHECK-NEXT: lit.alias.decl *"lt2{{.*}}:origin<false> {*"x`", *"y`1"}>>
|
||||
comptime lt2 = origin_of(x, y)
|
||||
# CHECK-NEXT: lit.alias.decl *"lt3{{.*}}:origin<true> *"z`2">>
|
||||
comptime lt3 = origin_of(z)
|
||||
# CHECK-NEXT: lit.alias.decl *"lt4{{.*}}:origin<false> {*"x`", (mutcast mut *"z`2")}>>
|
||||
comptime lt4 = origin_of(x, z)
|
||||
|
||||
|
||||
def take_string_var(var x: String, y: String) raises:
|
||||
# Check mutable to immutable origin conversions + inference.
|
||||
imm_ref_to[origin_of(x)](x)
|
||||
imm_ref_to(x)
|
||||
imm_ref_to[origin_of(y)](y)
|
||||
imm_ref_to(y)
|
||||
|
||||
|
||||
def imm_ref_to[origin: Origin[]](ref[origin] to: String):
|
||||
pass
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# in / not in
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_in
|
||||
def test_in(a: String, b: String):
|
||||
# CHECK-NEXT: [[SLICE:%.*]] = lit.call {{.*}}StringSpan::@"__init__{{.*}}(%a)
|
||||
# CHECK-NEXT: lit.call {{.*}}__contains__{{.*}}(%b, [[SLICE]])
|
||||
_ = a in b
|
||||
# CHECK: [[SLICE:%.*]] = lit.call {{.*}}StringSpan::@"__init__{{.*}}(%a)
|
||||
# CHECK-NEXT: [[RES:%.*]] = lit.call {{.*}}__contains__{{.*}}(%b, [[SLICE]])
|
||||
# CHECK-NEXT: [[RESB:%.*]] = lit.call {{.*}}__bool__{{.*}}([[RES]])
|
||||
# CHECK-NEXT: = lit.call {{.*}}__invert__{{.*}}([[RESB]])
|
||||
_ = a not in b
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# String literals
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_string_literal1
|
||||
def test_string_literal1(cond: Bool):
|
||||
_ = 4
|
||||
|
||||
# String literals should be fine at start of expression.
|
||||
# expected-warning @+1 {{'Bool' value is unused; assign to '_' to discard the result}}
|
||||
"a" == "abc"
|
||||
|
||||
# String literals should merge.
|
||||
var _ss: StaticString = "T" if cond else "F"
|
||||
|
||||
|
||||
# Issue #1850: Mojo assumes string literal at start of a function is a doc comment
|
||||
def test_expr_not_doc_string():
|
||||
# expected-warning @+1 {{'Bool' value is unused; assign to '_' to discard the result}}
|
||||
"a".__eq__("b")
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# MergeWith
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
struct TypeA(TrivialRegisterPassable):
|
||||
def __merge_with__[other_type: type_of(TypeB)](self) -> TypeB:
|
||||
pass
|
||||
|
||||
def __merge_with__[other_type: type_of(TypeC)](self) -> Int:
|
||||
pass
|
||||
|
||||
|
||||
struct TypeB(TrivialRegisterPassable):
|
||||
def __merge_with__[other_type: type_of(Int)](self) -> Int:
|
||||
pass
|
||||
|
||||
|
||||
struct TypeC(TrivialRegisterPassable):
|
||||
def __merge_with__[other_type: type_of(TypeA)](self) -> Int:
|
||||
pass
|
||||
|
||||
def __merge_with__[other_type: type_of(TypeD)](self) -> TypeE:
|
||||
pass
|
||||
|
||||
|
||||
struct TypeD(TrivialRegisterPassable):
|
||||
def __merge_with__[other_type: type_of(TypeA)](self) -> Int:
|
||||
pass
|
||||
|
||||
|
||||
struct TypeE(TrivialRegisterPassable):
|
||||
@implicit
|
||||
def __init__(out self, other: TypeD):
|
||||
pass
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_mergewith
|
||||
def test_mergewith(
|
||||
cond: __mlir_type.`!kgen.scalar<bool>`,
|
||||
a: TypeA,
|
||||
b: TypeB,
|
||||
c: TypeC,
|
||||
d: TypeD,
|
||||
):
|
||||
# One merges to the other.
|
||||
_ = a if cond else b
|
||||
# CHECK: hlcf.elif %cond
|
||||
# CHECK-NEXT: [[ARES:%.*]] = lit.call {{.*}}TypeA::@"__merge_with__
|
||||
# CHECK-NEXT: hlcf.yield [[ARES]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield %b
|
||||
# CHECK-NEXT: }
|
||||
|
||||
# This merge with two merge_with
|
||||
_ = a if cond else c
|
||||
# CHECK: hlcf.elif %cond
|
||||
# CHECK: [[ARES:%.*]] = lit.call {{.*}}TypeA::@"__merge_with__
|
||||
# CHECK: hlcf.yield [[ARES]]
|
||||
# CHECK: } else {
|
||||
# CHECK: [[CRES:%.*]] = lit.call {{.*}}TypeC::@"__merge_with__
|
||||
# CHECK: hlcf.yield [[CRES]]
|
||||
# CHECK: }
|
||||
|
||||
# One merge and one implicit conversion.
|
||||
_ = c if cond else d
|
||||
# CHECK: hlcf.elif %cond
|
||||
# CHECK: [[CRES:%.*]] = lit.call {{.*}}TypeC::@"__merge_with__
|
||||
# CHECK: hlcf.yield [[CRES]]
|
||||
# CHECK: } else {
|
||||
# CHECK: [[ARES:%.*]] = lit.call {{.*}}TypeE::@"__init__
|
||||
# CHECK: hlcf.yield [[ARES]]
|
||||
# CHECK: }
|
||||
|
||||
# Infer UValues from CValues.
|
||||
# https://github.com/modular/modular/issues/5239
|
||||
_ = Int() if cond else {}
|
||||
_ = {} if cond else Int()
|
||||
|
||||
# https://github.com/modular/modular/issues/5380
|
||||
# CHECK: [[FALSE:%.*]] = kgen.param.constant: scalar<bool> = <false>
|
||||
# CHECK-NEXT: [[COND:%.*]] = hlcf.elif [[FALSE]]
|
||||
# CHECK-NEXT: kgen.unreachable
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: = kgen.param.constant: !Bool = <{:scalar<bool> false}>
|
||||
# CHECK-NEXT: hlcf.yield
|
||||
# CHECK-NEXT: }
|
||||
# CHECK: hlcf.elif %{{.*}} {
|
||||
# expected-warning @+1 {{unreachable code on right side of 'False and ...'}}
|
||||
if False and cond:
|
||||
pass
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# Chained comparisons.
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"chained_cmp
|
||||
def chained_cmp(a: Int, b: Int, c: Int, d: Int, e: Int):
|
||||
# CHECK: [[CMP_A_B:%.*]] = lit.call tail @{{.*}}__lt__{{.*}}(%a, %b)
|
||||
# CHECK-NEXT: %[[CMP_A_B_SB:.*]] = lit.call tail @{{.*}}__mlir_bool__{{.*}}([[CMP_A_B]])
|
||||
# CHECK-NEXT: %[[IF_A_B:.*]] = hlcf.elif %[[CMP_A_B_SB]]
|
||||
# CHECK-NEXT: %[[CMP_B_C:.*]] = lit.call tail @{{.*}}__lt__{{.*}}(%b, %c)
|
||||
# CHECK: %[[IF_B_C:.*]] = hlcf.elif
|
||||
# CHECK-NEXT: %[[CMP_C_D:.*]] = lit.call tail @{{.*}}__lt__{{.*}}(%c, %d)
|
||||
# CHECK-NEXT: hlcf.yield %[[CMP_C_D]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield %[[CMP_B_C]]
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: hlcf.yield %[[IF_B_C]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield [[CMP_A_B]]
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: %res = lit.var.decl "res"
|
||||
# CHECK-NEXT: lit.ref.store %[[IF_A_B]], %res
|
||||
var res = a < b < c < d
|
||||
|
||||
# COM: This checks the parsing precedence between `<` and `and`.
|
||||
# CHECK: %[[CMP_A_B:.*]] = lit.call {{.*}}__lt__{{.*}}(%a, %b)
|
||||
# CHECK: %[[CMP_A_B_SB:.*]] = lit.call {{.*}}__mlir_bool__{{.*}}(%[[CMP_A_B]])
|
||||
# CHECK-NEXT: %[[IF_A_B:.*]] = hlcf.elif %[[CMP_A_B_SB]]
|
||||
# CHECK: %[[CMP_B_C:.*]] = lit.call {{.*}}__lt__{{.*}}(
|
||||
# CHECK-NEXT: hlcf.yield %[[CMP_B_C]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield %[[CMP_A_B]]
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: %[[CMP_SB:.*]] = lit.call {{.*}}__mlir_bool__{{.*}}(%[[IF_A_B]])
|
||||
# CHECK-NEXT: %[[IF:.*]] = hlcf.elif %[[CMP_SB]]
|
||||
# CHECK-NEXT: %[[CMP_D_E:.*]] = lit.call {{.*}}__lt__{{.*}}(%d, %e)
|
||||
# CHECK-NEXT: hlcf.yield %[[CMP_D_E]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield %[[IF_A_B]]
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: lit.ref.store %[[IF]], %res
|
||||
res = a < b < c and d < e
|
||||
|
||||
|
||||
# Test chained comparison op in parameter domain for issue
|
||||
# https://github.com/modularml/modular/issues/22050
|
||||
# CHECK: lit.alias.decl *"chainedCmpAlias1{{.*}}": !Bool ={{.*}}{:scalar<bool> false}
|
||||
comptime chainedCmpAlias1 = 1 == 2 == 3 == 4 == 5
|
||||
# CHECK: lit.alias.decl *"chainedCmpAlias2{{.*}}": !Bool ={{.*}}{:scalar<bool> true}
|
||||
comptime chainedCmpAlias2 = 1 <= 2 <= 3 <= 4 <= 5
|
||||
# CHECK: lit.alias.decl *"chainedCmpAlias3{{.*}}": !Bool ={{.*}}{:scalar<bool> false}
|
||||
comptime chainedCmpAlias3 = 1 <= 2 <= 9 <= 4 <= 5
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"chainedCmpSemiDyn
|
||||
def chainedCmpSemiDyn(x: Int, a: Int, b: Int, c: Int):
|
||||
# CHECK-NEXT: [[IFCOND:%.*]] = kgen.param.constant: scalar<bool> = <true>
|
||||
# CHECK-NEXT: [[FINALRESULT:%.*]] = hlcf.elif [[IFCOND]] -> !Bool {
|
||||
# CHECK-NEXT: [[PV:%.*]] = {{.*}}constant{{.*}}77
|
||||
# CHECK-NEXT: [[CMPRESULT1:%.*]] = {{.*}}__lt__{{.*}}([[PV]], %x)
|
||||
# CHECK-NEXT: [[IFCONDSB:%.*]] = {{.*}}__mlir_bool__{{.*}}([[CMPRESULT1]])
|
||||
# CHECK-NEXT: [[INNERRESULT:%.*]] = hlcf.elif [[IFCONDSB]] -> !Bool {
|
||||
# CHECK-NEXT: [[PV:%.*]] = {{.*}}constant{{.*}}105
|
||||
# CHECK-NEXT: [[CMPRESULT2:%.*]] = {{.*}}__lt__{{.*}}(%x, [[PV]])
|
||||
# CHECK-NEXT: [[IFCONDSB:%.*]] = {{.*}}__mlir_bool__{{.*}}([[CMPRESULT2]])
|
||||
# CHECK-NEXT: [[MOSTINNERRESULT:%.*]] = hlcf.elif [[IFCONDSB]] -> !Bool {
|
||||
# CHECK-NEXT: [[TRUEPARAM:%.*]] = kgen.param.constant: !Bool = {{.*}}{:scalar<bool> true}
|
||||
# CHECK-NEXT: hlcf.yield [[TRUEPARAM]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield [[CMPRESULT2]]
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: hlcf.yield [[MOSTINNERRESULT]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield [[CMPRESULT1]]
|
||||
# CHECK-NEXT: }
|
||||
# CHECK-NEXT: hlcf.yield [[INNERRESULT]]
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: [[TRUEPARAM:%.*]] = kgen.param.constant: !Bool = {{.*}}{:scalar<bool> true}
|
||||
# CHECK-NEXT: hlcf.yield [[TRUEPARAM]]
|
||||
# CHECK-NEXT: }
|
||||
# CHECK: [[XCMP:%.*]] = lit.var.decl "xCmp"
|
||||
# CHECK-NEXT: lit.ref.store [[FINALRESULT]], [[XCMP]]
|
||||
var xCmp = 5 < 77 < x < 105 < 177
|
||||
# A fully deep check of this would be a lot of work, but this at least
|
||||
# shows that its not choking during parsing on a mix of dynamic and
|
||||
# parameter comparisons. It required some care with the interaction
|
||||
# between recursive calls of emitNextCmp calls to get this to work.
|
||||
var mixedChain = 0 < 1 < a < 10 < 11 < b < 20 < 21 < c < 30 < 31
|
||||
|
||||
|
||||
# MOCO-3608: chained comparison with `in` should not require the RHS to be
|
||||
# ImplicitlyCopyable.
|
||||
struct NonCopyableContainer(Movable where False):
|
||||
def __contains__(self, x: Int) -> Bool:
|
||||
return False
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"chained_cmp_in_non_copyable
|
||||
def chained_cmp_in_non_copyable(a: Int, b: Int, c: NonCopyableContainer):
|
||||
# CHECK: [[CMP_A_B:%.*]] = lit.call {{.*}}__lt__{{.*}}(%a, %b)
|
||||
# CHECK: [[CMP_A_B_SB:%.*]] = lit.call {{.*}}__mlir_bool__{{.*}}([[CMP_A_B]])
|
||||
# CHECK-NEXT: {{%.*}} = hlcf.elif [[CMP_A_B_SB]]
|
||||
# CHECK-NEXT: {{%.*}} = lit.call {{.*}}__contains__{{.*}}(%c,
|
||||
# CHECK: hlcf.yield
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: hlcf.yield [[CMP_A_B]]
|
||||
# CHECK-NEXT: }
|
||||
_ = a < b in c
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# or/and
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
# MOCO-1987: Parser error when temporary PythonObject appears in or expression
|
||||
struct RPType(ImplicitlyCopyable, RegisterPassable):
|
||||
def __init__(out self):
|
||||
pass
|
||||
|
||||
def __bool__(self) -> Bool:
|
||||
return Bool()
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_rp_and_or
|
||||
def test_rp_and_or():
|
||||
# Evaluate the LHS, but materialize the rvalue into a memory slot.
|
||||
|
||||
# CHECK-NEXT: [[LHS:%.*]] = lit.call {{.*}}RPType::@"__init__()
|
||||
# CHECK-NEXT: [[TMPMEM:%.*]] = lit.var.decl "anonymous
|
||||
# CHECK-NEXT: lit.ref.store [[LHS]], [[TMPMEM]]
|
||||
|
||||
# CHECK-NEXT: [[IMMTMP:%.*]] = lit.ref.immut [[TMPMEM]]
|
||||
# CHECK-NEXT: lit.call {{.*}}RPType::@"__bool__{{.*}}([[IMMTMP]])
|
||||
# CHECK: hlcf.elif
|
||||
# CHECK-NEXT: [[LHS:%.*]] = lit.load.consume [[TMPMEM]]
|
||||
# CHECK-NEXT: hlcf.yield [[LHS]] : !RPType
|
||||
|
||||
_ = RPType() or RPType()
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# Keywords as identifiers
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
struct MatchExample(Movable where False):
|
||||
def match(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_match(a: MatchExample):
|
||||
a.match()
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# if/else expression
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
struct MoveOnly(Movable):
|
||||
pass
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"test_if_else_move
|
||||
def test_if_else_move(r: Bool, var a: MoveOnly, var b: MoveOnly):
|
||||
# This should move a/b into t.
|
||||
var t = b^ if r else a^
|
||||
|
||||
# CHECK: hlcf.elif
|
||||
# CHECK-NEXT: lit.ownership.use %b
|
||||
# CHECK-NEXT: lit.call {{.*}}__init__{{.*}}(%b, %t){{.*}}*, "move"
|
||||
# CHECK-NEXT: hlcf.yield
|
||||
# CHECK-NEXT: } else {
|
||||
# CHECK-NEXT: lit.ownership.use %a
|
||||
# CHECK-NEXT: lit.call {{.*}}__init__{{.*}}(%a, %t){{.*}}*, "move"
|
||||
# CHECK-NEXT: hlcf.yield
|
||||
# CHECK-NEXT: }
|
||||
|
||||
|
||||
def test_contextual_if[cond: Bool]():
|
||||
comptime some_type: Movable = Int if cond else String
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# comptime expression
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
struct NotRuntimeMaterializable(Movable where False):
|
||||
def method(self) -> Int:
|
||||
pass
|
||||
|
||||
|
||||
def test_comptime_expression[nrm: NotRuntimeMaterializable]():
|
||||
# Ok to materialize an int.
|
||||
var b = comptime (nrm.method())
|
||||
|
||||
|
||||
##===----------------------------------------------------------------------===##
|
||||
# Ellipsis.
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
|
||||
def test_ellipsis_overloading(a: Int):
|
||||
pass
|
||||
|
||||
|
||||
def test_ellipsis_overloading(a: EllipsisType):
|
||||
pass
|
||||
|
||||
|
||||
def test_ellipsis():
|
||||
var x = ...
|
||||
test_ellipsis_overloading(4)
|
||||
test_ellipsis_overloading(...)
|
||||
@@ -0,0 +1,63 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# 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 -verify-diagnostics
|
||||
|
||||
|
||||
struct WeirdArray(Movable where False):
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def __getitem__(self, x: Int) -> Int:
|
||||
return x
|
||||
|
||||
|
||||
def test_getitem(var a: WeirdArray, f: String, x: Int):
|
||||
# expected-error @+1 {{invalid call to '__getitem__': value passed to 'x' cannot be converted from 'String' to 'Int'}}
|
||||
_ = a[f]
|
||||
|
||||
# expected-error @+1 {{invalid call to '__getitem__': unexpected argument}}
|
||||
_ = a[x, x]
|
||||
|
||||
# expected-error @+1 {{expression must be mutable in assignment}}
|
||||
a[x] = x
|
||||
|
||||
|
||||
struct NotSettable(Movable where False):
|
||||
def __getitem__(self) -> Int:
|
||||
pass
|
||||
|
||||
# expected-error @+1 {{__setitem__ must take at least one argument for the value to set}}
|
||||
def __setitem__(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_setitem_kwargs(ns: NotSettable, x: Int):
|
||||
# expected-note @+1 {{used in an expression here}}
|
||||
ns[] = x
|
||||
|
||||
@fieldwise_init
|
||||
struct VariadicIndexList(Movable where False):
|
||||
def __getitem__(mut self, *indices: Int) -> Int:
|
||||
pass
|
||||
|
||||
def __setitem__(mut self, *indices: Int, val: Int):
|
||||
pass
|
||||
|
||||
|
||||
# CHECK-LABEL: lit.fn @"testVariadicIndexList
|
||||
# MOCO-696: Support variadic length keys in __setitem__
|
||||
def testVariadicIndexList(mut foo: VariadicIndexList, i: Int, the_value: Int):
|
||||
# Getter is straight-forward.
|
||||
_ = foo[i, i]
|
||||
|
||||
# Setter needs to pass the new value as 'val', not in the variadics.
|
||||
foo[i, i, i, i] = the_value
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# 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 --verify-diagnostics
|
||||
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# __is_run_in_comptime_interpreter
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
|
||||
|
||||
def test_with_comptime_if():
|
||||
#expected-error@+1 {{cannot use a dynamic value in 'comptime if' condition}}
|
||||
comptime if __is_run_in_comptime_interpreter:
|
||||
var x: Int
|
||||
|
||||
def test_as_comptime_expression[b: Bool]():
|
||||
#expected-error@+1 {{cannot use a dynamic value in comptime initializer}}
|
||||
comptime i = __is_run_in_comptime_interpreter and b:
|
||||
@@ -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 -split-input-file %s
|
||||
|
||||
|
||||
# The octal escape sequence in string literals \ooo can have variable length.
|
||||
def testOctal():
|
||||
comptime x = "A\0"
|
||||
comptime y = "A\01"
|
||||
comptime z = "A\012"
|
||||
|
||||
|
||||
# // -----
|
||||
|
||||
|
||||
def testTripleQuote():
|
||||
# expected-error @below {{invalid escape sequence}}
|
||||
var x = """$\s$"""
|
||||
|
||||
|
||||
# // -----
|
||||
|
||||
|
||||
# Unicode \u escape requires exactly four hex digits.
|
||||
def testUnicodeLittleUTooFewDigits():
|
||||
# expected-error @below {{\u requires exactly four hex digits}}
|
||||
var x = "\u006"
|
||||
|
||||
|
||||
# // -----
|
||||
|
||||
|
||||
# Unicode \U escape requires exactly eight hex digits.
|
||||
def testUnicodeBigUTooFewDigits():
|
||||
# expected-error @below {{\U requires exactly eight hex digits}}
|
||||
var x = "\U0001F60"
|
||||
|
||||
|
||||
# // -----
|
||||
|
||||
|
||||
# \U value exceeding U+10FFFF is rejected.
|
||||
def testUnicodeBigUOutOfRange():
|
||||
# expected-error @below {{value must not exceed U+10FFFF}}
|
||||
var x = "\U00110000"
|
||||
|
||||
|
||||
# // -----
|
||||
|
||||
|
||||
# Surrogates are not valid Unicode scalar values.
|
||||
def testUnicodeSurrogate():
|
||||
# expected-error @below {{unicode escape sequences do not support surrogate code points (U+D800 to U+DFFF); use '\U' with the full code point (not a UTF-16 surrogate pair)}}
|
||||
var x = "\uD800"
|
||||
|
||||
|
||||
# // -----
|
||||
|
||||
|
||||
# Named unicode escapes \N{name} are not supported.
|
||||
def testUnicodeNamedEscape():
|
||||
# expected-error @below {{invalid escape sequence}}
|
||||
var x = "\N{SNOWMAN}"
|
||||
@@ -0,0 +1,46 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# 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 -verify-diagnostics
|
||||
|
||||
# MOCO-3472: a non-empty tuple literal used in type position must be
|
||||
# diagnosed directly, instead of falling through to Tuple's constructor-call
|
||||
# machinery and failing with a confusing trait-conformance error.
|
||||
|
||||
|
||||
# expected-error @+1 {{expected a type, found a tuple value; use 'Tuple[...]' to write a tuple type}}
|
||||
def returns_tuple_type() -> (Int, Int):
|
||||
return (Int(1), Int(2))
|
||||
|
||||
|
||||
def takes_tuple_type_param(
|
||||
# expected-error @+1 {{expected a type, found a tuple value; use 'Tuple[...]' to write a tuple type}}
|
||||
x: (Int, Int)
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def local_tuple_type_annotation():
|
||||
# expected-error @+1 {{expected a type, found a tuple value; use 'Tuple[...]' to write a tuple type}}
|
||||
var x: (Int, Int)
|
||||
|
||||
|
||||
def single_element_tuple_type():
|
||||
# expected-error @+1 {{expected a type, found a tuple value; use 'Tuple[...]' to write a tuple type}}
|
||||
var x: (Int,)
|
||||
|
||||
|
||||
# The empty tuple is the one legitimate type/value ambiguity and must
|
||||
# continue to work as sugar for 'Tuple[]'.
|
||||
def returns_empty_tuple_type() -> ():
|
||||
return ()
|
||||
@@ -0,0 +1,199 @@
|
||||
# ===----------------------------------------------------------------------=== #
|
||||
# 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 -verify-diagnostics
|
||||
|
||||
|
||||
struct RP_NotTrivial(RegisterPassable):
|
||||
pass
|
||||
|
||||
|
||||
struct Foo(Movable where False):
|
||||
def __init__(out self):
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def take_instance_param[a: Foo]():
|
||||
pass
|
||||
|
||||
|
||||
# expected-note @+1 {{function declared here}}
|
||||
def takes_instance_arg(a: Foo):
|
||||
pass
|
||||
|
||||
|
||||
# COM: Issue #27654: Parser crash: Assertion failed: Types should match
|
||||
# COM: https://github.com/modular/mojo/issues/1607 Improved error message for this common error
|
||||
def test_type_instead_of_instance() -> Foo:
|
||||
# expected-error @+1 {{'take_instance_param' parameter 'a' has 'Foo' type, but value has type 'AnyStruct[Foo]'}}
|
||||
take_instance_param[Foo]
|
||||
# expected-error @+1 {{invalid call to 'takes_instance_arg': value passed to 'a' cannot be converted from type value 'Foo' to an instance of 'Foo'; did you mean to instantiate 'Foo'?}}
|
||||
takes_instance_arg(Foo)
|
||||
# expected-error @+1 {{cannot implicitly convert 'Foo' type as a value to an instance of 'Foo'; did you mean to instantiate 'Foo'?}}
|
||||
return Foo
|
||||
|
||||
|
||||
# COM: https://github.com/modularml/modular/issues/29438
|
||||
# COM: ensure we do not crash in the example below, but emit an error.
|
||||
struct MadeFromPack[*Ts: AnyType](Movable where False):
|
||||
@implicit
|
||||
def __init__(out self, *args: * Self.Ts):
|
||||
pass
|
||||
|
||||
|
||||
struct WrapsMadeFromPack[*Ts: AnyType](Movable where False):
|
||||
var data: MadeFromPack[*Self.Ts]
|
||||
|
||||
@implicit
|
||||
def __init__(out self, *args: * Self.Ts):
|
||||
# expected-error @+1 {{cannot implicitly convert 'VariadicPack[False, Ts]' value to 'MadeFromPack[Ts]'}}
|
||||
self.data = args
|
||||
|
||||
|
||||
struct Constructible(Movable where False):
|
||||
@implicit
|
||||
def __init__(out self, arg: Int):
|
||||
pass
|
||||
|
||||
|
||||
def init_self_conversion():
|
||||
# expected-error @below {{cannot implicitly convert 'def __init__(arg: Int) thin -> Constructible' value to 'def() thin -> None'}}
|
||||
comptime f: def() thin -> None = Constructible.__init__
|
||||
|
||||
|
||||
struct ConvertibleFromInt(ImplicitlyCopyable):
|
||||
@implicit
|
||||
def __init__(out self, arg: Int):
|
||||
pass
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
# expected-note @below {{candidate declared here}}
|
||||
# expected-note @below {{def __init__(out self, var a: ConvertibleFromInt, b: Int) # note - generated function}}
|
||||
struct AmbiguousCtor(Movable where False):
|
||||
var a: ConvertibleFromInt
|
||||
var b: Int
|
||||
|
||||
# expected-note @below {{candidate declared here}}
|
||||
def __init__(out self, b: Int, a: ConvertibleFromInt):
|
||||
pass
|
||||
|
||||
|
||||
struct AlsoConvertibleFromInt(Movable where False):
|
||||
@implicit
|
||||
def __init__(out self, arg: Int):
|
||||
pass
|
||||
|
||||
|
||||
struct AmbiguousConversion(Movable where False):
|
||||
@implicit
|
||||
# expected-note @below {{candidate declared here}}
|
||||
def __init__(out self, x: ConvertibleFromInt):
|
||||
pass
|
||||
|
||||
@implicit
|
||||
# expected-note @below {{candidate declared here}}
|
||||
def __init__(out self, x: AlsoConvertibleFromInt):
|
||||
pass
|
||||
|
||||
|
||||
def ambiguous_ctor_call(x: Int):
|
||||
# expected-error @below {{ambiguous call}}
|
||||
AmbiguousCtor(x, x)
|
||||
|
||||
# expected-error @below {{ambiguous call to '__init__', each candidate requires 1 implicit conversion}}
|
||||
AmbiguousConversion(x)
|
||||
|
||||
|
||||
# MOCO-990: Conditional conformance trick fails on SIMD constructor from Bool
|
||||
struct MySIMD[value: Int](Movable where False):
|
||||
def __init__(out self: MySIMD[0], val: MyBool):
|
||||
pass
|
||||
|
||||
|
||||
struct MyBool(Movable where False):
|
||||
@implicit
|
||||
def __init__(out self, value: MySIMD[0]):
|
||||
pass
|
||||
|
||||
|
||||
def test_bad_conversion(a: MySIMD[0]):
|
||||
# expected-error @+1 {{cannot implicitly convert 'MySIMD[Int(0)]' value to 'MySIMD[Int(1)]'}}
|
||||
var b: MySIMD[1] = a
|
||||
|
||||
|
||||
# MOCO-1090: bad parameter inference error message.
|
||||
def test_rp_trivial_inference(a: RP_NotTrivial, b: Foo):
|
||||
# expected-error @below {{invalid call to 'infer_rp_trivial': value passed to 'val' cannot be converted from 'RP_NotTrivial' to 'T', argument type 'RP_NotTrivial' does not conform to trait 'TrivialRegisterPassable'}}
|
||||
_ = infer_rp_trivial(a)
|
||||
|
||||
# expected-error @below {{invalid call to 'infer_rp_trivial': value passed to 'val' cannot be converted from 'Foo' to 'T', argument type 'Foo' does not conform to trait 'TrivialRegisterPassable'}}
|
||||
_ = infer_rp_trivial(b)
|
||||
|
||||
|
||||
# expected-note @below {{function declared here}}
|
||||
def infer_rp_trivial[T: TrivialRegisterPassable](val: T):
|
||||
pass
|
||||
|
||||
|
||||
struct ImplicitFromInt:
|
||||
@implicit
|
||||
def __init__(out self, value: Int):
|
||||
pass
|
||||
|
||||
|
||||
def stripping_raises():
|
||||
def fn_raises() raises:
|
||||
pass
|
||||
|
||||
def fn_raises_bool(arg: Int) raises Bool:
|
||||
pass
|
||||
|
||||
def fn_raises_int(arg: Int) raises Int:
|
||||
pass
|
||||
|
||||
# expected-error @+1 {{cannot implicitly convert 'def fn_raises() raises thin -> None' value to 'def() thin -> None'}}
|
||||
var fp: def() thin = fn_raises
|
||||
|
||||
# expected-error @+2 {{cannot implicitly convert 'def fn_raises() raises thin -> None' value to 'def() raises Int thin -> None'}}
|
||||
# expected-note @+1 {{error type of the first type is 'Error' but the second type is 'Int'}}
|
||||
var fp2: def() thin raises Int = fn_raises
|
||||
|
||||
# expected-error @+3 {{cannot implicitly convert 'def fn_raises_bool(arg: Int) raises Bool thin -> None' value to 'def(*args: *TypeList[Int]()) raises Int thin -> None' in 'var' initializer}}
|
||||
var fp3: def(
|
||||
*args: *TypeList.splat[1, Int]()
|
||||
) raises Int thin = fn_raises_bool
|
||||
|
||||
# Just because the RHS `raises` type is implicitly convertible to the LHS
|
||||
# `raises`, that does not imply that the function types should be implicitly
|
||||
# convertible. Maybe someday.
|
||||
# expected-error @+3 {{cannot implicitly convert 'def fn_raises_int(arg: Int) raises Int thin -> None' value to 'def(*args: *TypeList[Int]()) raises ImplicitFromInt thin -> None' in 'var' initializer}}
|
||||
var fp4: def(
|
||||
*args: *TypeList.splat[1, Int]()
|
||||
) raises ImplicitFromInt thin = fn_raises_int
|
||||
|
||||
struct SimplePair(Copyable):
|
||||
var left: Int
|
||||
var right: Int
|
||||
|
||||
def test_field_erase(ref p: SimplePair, cond: Bool) -> ref[origin_of(p).subtree] Int:
|
||||
# This is ok. The origin union for left/right type erase.
|
||||
return p.left if cond else p.right
|
||||
|
||||
var x: Int
|
||||
# expected-error @+1 {{cannot return reference with incompatible origin: 'origin_of(x)' vs 'origin_of(origin_of(p).subtree)'}}
|
||||
return x
|
||||
|
||||
def test_field_erase_2(ref p: List[SimplePair]) -> ref[origin_of(p).subtree] Int:
|
||||
return p[0].left
|
||||
Reference in new issue
Block a user