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

Function definition is ignored #92

Closed
ytoml opened this issue Aug 29, 2022 · 5 comments · Fixed by #96 or #99
Closed

Function definition is ignored #92

ytoml opened this issue Aug 29, 2022 · 5 comments · Fixed by #96 or #99
Labels
bug Something isn't working bug-compiler bug-parser

Comments

@ytoml
Copy link
Contributor

ytoml commented Aug 29, 2022

Describe the bug
Function declaration ignored, i.e. like encountering NameError on f(1) after f x = x.

Reproducible code

f x = x
f
# compiler complains even f is defined.
Error[#0213]: File <stdin>, line 1, in <module>
1│ f(1)
   ^
NameError: f is not defined

Expected behavior
Call f.

Additional context
Actually I found it's originated in Desugarer::desugar_pattern.
Its matching ignores Expr::Def(Def { sig: Signature::Subr(v)..}) like:

while let Some(chunk) = module.lpop() {
    match chunk {
        Expr::Def(def) => {
            if let Signature::Var(v) = def.sig {
                /* desugaring */
            } // if end
        },
        other => new.push(other),
    } // match end
} 

Thus, function parsed as Subr vanishes there.

However just fixing this as

match chunk {
    Expr::Def(Def { sig: Signature::Var(v) .. }) => { /* omit */ }
    other => /* omit */
}

produces stack overflow (eternal recursion) in compile process.
Maybe compiler also have some bug as the eternal recursion is not desireble if it's fatal error, but we have to add desugaring logic for Signature::Subr here first, rather than just passing it through this matching, right?

If so, I'm willing to implement them as it seems to be good first implementation to get used to Erg's syntax.
(And maybe fixing compiler bug is also not difficult and I'm willing to do it too.)

@ytoml ytoml added the bug Something isn't working label Aug 29, 2022
@ytoml
Copy link
Contributor Author

ytoml commented Aug 30, 2022

Maybe compiler also have some bug as the eternal recursion is not desireble if it's fatal error, but we have to add desugaring logic for Signature::Subr here first, rather than just passing it through this matching, right?

Maybe I was wrong because desugar_pattern seems (by its name) to be just for desugaring pattern matching statements but not functions or procedures.

@ytoml
Copy link
Contributor Author

ytoml commented Aug 30, 2022

Now I'm determined to keep this pattern matching as it is except for ignoring Subr, and look into compiler to debug eternal recursion.

@mtshiba
Copy link
Member

mtshiba commented Aug 30, 2022

You are correct, this bug is due to a leak in the pattern match. As far as I can see in the debug mode, your fix solves this issue, and the stack overflow seems to be a type inference bug, not a parser bug.

In the meantime, could you please send me a PR that fixes this bug?

@mtshiba mtshiba mentioned this issue Aug 30, 2022
@mtshiba mtshiba reopened this Aug 30, 2022
@mtshiba
Copy link
Member

mtshiba commented Aug 30, 2022

I fixed the infinite recursion issue in #96.

@ytoml
Copy link
Contributor Author

ytoml commented Aug 30, 2022

Thank you for information! I will open PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bug-compiler bug-parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants