Sitelet https://github.com/argotorg/fe/pull/1780
Skip to content

Infer generic lengths from string literals - #1780

Open
cburgdorf wants to merge 1 commit into
masterfrom
fix/string-literal-const-generic-inference
Open

cburgdorf wants to merge 1 commit into
masterfrom
fix/string-literal-const-generic-inference

Conversation

@cburgdorf

Copy link
Copy Markdown
Collaborator

A string literal passed to String<N> or [u8; N] with a generic N left N unresolved ("type annotation is needed"):

  • String<N>: unifying the literal's string variable with String<?N> accepted the type without binding anything, so the literal later fell back to String<len> while ?N stayed open. Now the length variable gets the new TyVarSort::StringLen { min_len } sort and the literal is bound to String<?N>. The sort only unifies with constants of at least min_len (several literals keep the largest minimum) and defaults to min_len when types are finalized.
  • [u8; N]: by-value parameters are typed as view [u8; N], so the check that types a literal as a byte array for a [u8; _] expectation never saw the array. Peel the view capability first.

A string literal passed to `String<N>` or `[u8; N]` with a generic `N` left
`N` unresolved ("type annotation is needed"):

- `String<N>`: unifying the literal's string variable with `String<?N>`
  accepted the type without binding anything, so the literal later fell back
  to `String<len>` while `?N` stayed open. Now the length variable gets the
  new `TyVarSort::StringLen { min_len }` sort and the literal is bound to
  `String<?N>`. The sort only unifies with constants of at least `min_len`
  (several literals keep the largest minimum) and defaults to `min_len` when
  types are finalized.
- `[u8; N]`: by-value parameters are typed as `view [u8; N]`, so the check
  that types a literal as a byte array for a `[u8; _]` expectation never saw
  the array. Peel the view capability first.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-05T08:26:43.997877Z b5ec879 PR opened
🔒 Security Review ✅ Completed 2026-10-05T08:25:27.372194Z b5ec879 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: b5ec879270

ℹ️ 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 on lines +553 to +561
TyVarSort::StringLen { min_len } => {
if let TyData::ConstTy(const_ty) = value.data(self.db)
&& let Some(n_value) = const_ty.integer_value(self.db)
&& n_value < BigInt::from(min_len)
{
return Err(UnificationError::TypeMismatch);
}
self.table
.unify_var_value(root_var.key, InferenceValue::Bound(value))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve the minimum when binding symbolic lengths

When another argument equates this inferred length to a caller's symbolic const parameter, integer_value returns None, so this arm binds the variable while discarding min_len. For example, f("abc", a) for parameters String<N> and [u8; N] inside a generic caller taking a: [u8; M] binds N to unconstrained M; instantiating that caller with M = 2 then treats the three-byte literal as String<2>. Reject symbolic values unless the lower bound is proven, or propagate the minimum constraint instead of unconditionally binding them.

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
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.

1 participant