modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.92k stars 2.58k forks source link

[BUG] param_env (string_literal, stringref, _memmem) #2434

Open rd4com opened 4 months ago

rd4com commented 4 months ago

Bug description

edit: see first comment of the github issue

 

Hello,

i was working on an hopefully really helpful tutorial :+1: and after adding a constrained:

Example:

from sys import param_env

alias DefaultError =
    param_env.env_get_string['error_message']() 

def main():
    alias MakeSureThat = constrained

    MakeSureThat[    
        "error" in DefaultError,
        "if not, unacceptable error message:"+DefaultError
    ]()

    MakeSureThat[   
        len(DefaultError)>5 ,
        "if not, len<=5 can't be a meaningful error message"
    ]()

    MakeSureThat[    
        len(DefaultError)<32,
        "if not, len>=32 might be too long and could be confusing"
    ]()

    try:
        print("Pretty confident about the quality of the error message")
        raise DefaultError 
    except e:
        print("The error is:",e)

Error:

mojo run -D error_message="this is the default error message, it is kind of long in order to test the constrained" test_.mojo

 /__w/modular/modular/Kernels/mojo/stdlib/builtin/string_literal.mojo:163:8: note:                     failed to interpret function @stdlib::builtin::string_literal::StringLiteral::__contains__(stdlib::builtin::string_literal::StringLiteral,stdlib::builtin::string_literal::StringLiteral)
/__w/modular/modular/Kernels/mojo/stdlib/builtin/string_literal.mojo:172:23: note:                       failed to evaluate call
/__w/modular/modular/Kernels/mojo/stdlib/utils/stringref.mojo:248:8: note:                         failed to interpret function @stdlib::utils::stringref::StringRef::__contains__(stdlib::utils::stringref::StringRef,stdlib::utils::stringref::StringRef)
/__w/modular/modular/Kernels/mojo/stdlib/utils/stringref.mojo:257:25: note:                           failed to evaluate call
/__w/modular/modular/Kernels/mojo/stdlib/utils/stringref.mojo:259:8: note:                             failed to interpret function @stdlib::utils::stringref::StringRef::find(stdlib::utils::stringref::StringRef,stdlib::utils::stringref::StringRef,stdlib::builtin::int::Int)
/__w/modular/modular/Kernels/mojo/stdlib/utils/stringref.mojo:280:26: note:                               failed to evaluate call
/__w/modular/modular/Kernels/mojo/stdlib/utils/stringref.mojo:421:4: note:                                 failed to interpret function @stdlib::utils::stringref::_memmem[stdlib::builtin::dtype::DType](stdlib::memory::unsafe::DTypePointer[$0, {{0}}],stdlib::builtin::int::Int,stdlib::memory::unsafe::DTypePointer[$0, {{0}}],stdlib::builtin::int::Int),type=si8
/__w/modular/modular/Kernels/mojo/stdlib/memory/unsafe.mojo:152:6: note:                                   failed to fold operation pop.bitcast(#pop.simd<false, false, false, false, false, false, false, false, false, false, true, false, false, true, false, false, false, false, false, false, true, false, false, false, false, false, false, true, false, false, false, false> : !pop.simd<32, bool>)

Steps to reproduce

Nothing special, it is in the bug description

System information

- Ubuntu 23.10
- mojo 2024.4.2621 (6dff3016)
- modular 0.7.2 (d0adc668)
rd4com commented 4 months ago

update: this seem to be the part that cause the bug

from sys import param_env

alias DefaultError =
    param_env.env_get_string['error_message']() 

def main():
    alias MakeSureThat = constrained

    MakeSureThat[    
        "error" in DefaultError,
        "if not, unacceptable error message:"+DefaultError
    ]()

    raise DefaultError

mojo run -D error_message="this is the default error message, it is kind of long in order to test the constrained" test_.mojo

Same error as in description

 

But it works well (meaning no error) with smaller inputs: mojo build -D error_message="the error message is small" test_.mojo

 

Tried to find a bug starting from there, but could not:

 fn __contains__(self, substr: StringLiteral) -> Bool:
     return substr in StringRef(self)

https://github.com/modularml/mojo/blob/nightly/stdlib/src/builtin/string_literal.mojo