Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Use of unknown declaration in a for loop elif block #3709

Open
bpo217 opened this issue Oct 24, 2024 · 2 comments
Open

[BUG] Use of unknown declaration in a for loop elif block #3709

bpo217 opened this issue Oct 24, 2024 · 2 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@bpo217
Copy link

bpo217 commented Oct 24, 2024

Bug description

Screenshot 2024-10-24 at 06 53 14 Screenshot 2024-10-24 at 06 54 41 Screenshot 2024-10-24 at 06 56 08

All the code

src/colcat.mojo

import sys
import sys.arg as cli
import tag


trait CliEventHandler:
    fn toggle_hyphenate(inout self) raises:
        ...


@value
struct App(CliEventHandler):
    var hyphenate: Bool

    fn toggle_hyphenate(inout self) raises:
        if self.hyphenate:
            raise Error("You passed `--hyphenate` twice.")
        else:
            self.hyphenate = True
        

fn base_arg[T: CliEventHandler](
    inout app: T, 
    borrowed arg: String,
) raises -> Int:
    if arg == "--col-width":
        return tag.parse_col_width
    elif arg == "--hyphenate":
        try:
            app.toggle_hyphenate() 
        except e:
            print(e, file=sys.stderr)
        return tag.stop
    else:
        raise Error("The argument `" + arg + "` is undefined.")
     

fn main():
    var app = App(hyphenate=False)
    var state: Int = tag.skip
    for arg in cli.argv():
        if state == tag.do_base_arg:
            try:
                state = base_arg(app, arg)
            except e:
                print(e, file=sys.stderr)
                return
        elif state == tag.stop:
            return
        elif state == tag.skip:
            state = tag.do_base_arg
            continue
        else:
            print("OMG")
            return

src/tag/__init.mojo

"""
Implements the tag package.
The tag package represents the app's events in its timeline as tags.
"""
from .constants import do_base_arg, parse_col_width, print_help, stop, skip

src/tag/constants.mojo

alias do_base_arg = 0
alias parse_col_width = 1
alias print_help = 2
alias stop = 3
alias skip = 4

Steps to reproduce

  1. Create a mojo project with mojo init --format mojoproject.
  2. Write some code like I provided.

System information

System: Macbook Air M2 2023 on Sonoma 14.5
mojo --version: `mojo 24.4.0 (2cb57382)`
magic -V: `magic 0.4.0 - (based on pixi 0.33.0)`
magic info:

     Magic version: 0.4.0
System
------------
      Pixi version: 0.33.0
          Platform: osx-arm64
  Virtual packages: __unix=0=0
                  : __osx=14.5=0
                  : __archspec=1=m2
         Cache dir: /Users/bpo/Library/Caches/rattler/cache
      Auth storage: /Users/bpo/.rattler/credentials.json
  Config locations: No config files found

Global
------------
           Bin dir: /Users/bpo/.modular/bin
   Environment dir: /Users/bpo/.modular/envs
      Manifest dir: /Users/bpo/.modular/manifests/pixi-global.toml

Project
------------
              Name: colcat-mojo
           Version: 0.1.0
     Manifest file: /Users/bpo/Projects/colcat-mojo/mojoproject.toml
      Last updated: 23-10-2024 19:24:21

Environments
------------
       Environment: default
          Features: default
          Channels: conda-forge, https://conda.modular.com/max
  Dependency count: 0
  Target platforms: osx-arm64
@bpo217 bpo217 added bug Something isn't working mojo-repo Tag all issues with this label labels Oct 24, 2024
@soraros
Copy link
Contributor

soraros commented Oct 24, 2024

We can further reduce the repro.

# bug.mojo

import tag

fn main():
    state = tag.A
# tag.mojo

alias A = 0

@bpo217
Copy link
Author

bpo217 commented Oct 24, 2024

We can further reduce the repro.

# bug.mojo

import tag

fn main():
    state = tag.A
# tag.mojo

alias A = 0

Actually, because I assumed it so my bad. This is not the same bug. I thought in a fn declaration mojo var is required. So this isn't the same thing. What would be the same thing is this:

# bug.mojo
import tag

fn main():
    var state = tag.A
    state = tag.B
# tag.mojo
alias A = 0
alias B = 1

Don't you think? Also this way does indeed reproduce the correct error that I was seeing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

2 participants