Skip to content

checker: reject unresolved types used in interface match/is branches - #27694

Open
rilaaax wants to merge 5 commits into
vlang:masterfrom
rilaaax:checker/fix-interface-match-unknown-type
Open

checker: reject unresolved types used in interface match/is branches#27694
rilaaax wants to merge 5 commits into
vlang:masterfrom
rilaaax:checker/fix-interface-match-unknown-type

Conversation

@rilaaax

@rilaaax rilaaax commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #26972

Problem

Matching on an interface value with a branch type that was never actually declared (e.g. a typo, or a leftover/renamed type) compiles without any V error, but crashes the underlying C compiler with a confusing error instead:

cc: ...: error: unknown type name 'main__Nil'

Minimal repro:

interface Value {}

fn (v Value) get_str() string {
    return match v {
        Nil { '' }      // `Nil` is never declared anywhere
        else { '' }
    }
}

Cause

The parser registers an unresolved type name like Nil as a .placeholder type symbol (the normal mechanism used to support forward references). The checker is supposed to reject any .placeholder that's still unresolved by the time it's actually used, but type_implements_with_mut_receiver() in checker.v never checked for this. It validates a type against an interface by iterating over the interface's required methods/fields; for an interface with few or none of those (e.g. an empty interface Value {}), the loop does nothing and the function returns true by default — regardless of whether the type it was given actually exists. The bogus placeholder type then flows unchecked into cgen, which generates casts and variant-index machinery for a struct that was never defined, and only cc catches it.

Fix

  • checker.v: type_implements_with_mut_receiver() now returns false immediately when given a .placeholder type, without erroring itself (some callers already have their own dedicated handling for this case).
  • match.v: the interface branch of match_exprs() now reports unknown type '<name>' when the branch type doesn't implement the interface and is unresolved, mirroring the existing sum type variant error.
  • infix.v: the is/as interface check is skipped when the type is a placeholder, since that case is already reported a few lines above, avoiding a duplicate error message.

Test

Regression test added.

@medvednikov

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 207abfa224

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/checker/checker.v Outdated
@rilaaax
rilaaax force-pushed the checker/fix-interface-match-unknown-type branch from 1ebba3a to d85f921 Compare July 9, 2026 13:27
@GGRei GGRei closed this Jul 11, 2026
@GGRei GGRei reopened this Jul 11, 2026
@GGRei

GGRei commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d85f921627

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/checker/checker.v Outdated
@rilaaax
rilaaax force-pushed the checker/fix-interface-match-unknown-type branch from d85f921 to f1c1a6e Compare July 15, 2026 12:37
@GGRei

GGRei commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

The direct unresolved type case is fixed. To address the second Codex review, the checker still needs to reject unresolved placeholders inside generic instantiations, such as Box[Missing], with a regression test covering that case.

Thanks!

@rilaaax
rilaaax force-pushed the checker/fix-interface-match-unknown-type branch from f1c1a6e to 0df3526 Compare July 18, 2026 15:25
@GGRei

GGRei commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c80b3b2c37

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/checker/checker.v Outdated
Comment thread vlib/v/parser/parse_type.v Outdated
Comment thread vlib/v/checker/infix.v Outdated
@GGRei

GGRei commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

One Codex point is still only partially fixed. I reproduced it on a clean checkout: Box[fn (Missing) int] still reaches C generation and fails with an unknown type error. Please extend the unresolved-type traversal to ast.FnType parameters and return types, and add a regression test before closing “Recurse into container type arguments.”

Thanks!

@rilaaax
rilaaax force-pushed the checker/fix-interface-match-unknown-type branch from fe6bf7b to 86dcad0 Compare July 28, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cgen error: could not generate string method redict__Nil_str for type redict__Nil

3 participants