Skip to content

check: a u64 from a call that took a key does not point at it - #4431

Open
fengqiaoqu wants to merge 2 commits into
vx-lang:mainfrom
fengqiaoqu:fix/e4005-int-result-4428
Open

fengqiaoqu wants to merge 2 commits into
vx-lang:mainfrom
fengqiaoqu:fix/e4005-int-result-4428

Conversation

@fengqiaoqu

Copy link
Copy Markdown
Contributor

Fixes #4428.

map_hash(&key) returns a u64. The escape check treated that result as if it pointed at key, and then refused self.set_ctrl_byte(i, map_tag(h) as u8) with E4005 when the key type itself holds a reference. A u64 cannot hold a reference, so it does not point at anything.

Two changes:

  • ref_provenance_of returns nothing when the expression's type is known and cannot hold a reference.
  • The type probe uses the type a cast names, so h as u8 is a u8, not unknown (unknown was treated as "could hold a reference").

I reproduced the error on the put_new sketch in the issue (E4005 at hash_map.vx:339:10) and on the reduced test in this PR. After the fix, both compile. Existing E4005 fail tests I ran still report the error.

A call given a reference to a key can return an integer. The escape
check used to keep the provenance of that key on the result. A later
integer cast, whose type the probe also missed, was then refused as if
it could be stored through self (E4005, vx-lang#4428).

A value whose type cannot hold a reference does not point at anything.
The type probe now sees the type a cast names.
@hiraditya

hiraditya commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Review of this PR. Finding 1 comes from reading the code. Didn't built the PR or ran the example.

Correctness

  1. Possible regression: raw pointers made from a local. The new check at the top of ref_provenance_of (src/hir/check/access.rs:2423) uses type_can_hold_reference. That function gives false for *mut i32 and other pointers to plain data. So each path that follows a reference through a raw pointer made from a local gives None instead of Local. This return would no longer give E4005:

    fn f() -> &mut i32 { let mut x = 5; return unsafe { &mut *(&mut x as *mut i32) }; }
    

    The same holds for return deref_it(&mut x as *mut i32), where join_arg_provenance skips the argument, and for the provenance that tensor_view_2d(&mut buf[0] as *mut f32, ..) stores in foreign_views. No fail test covers these cases.

  2. The root cause of check: E4005 says a u64 made from &key points into the stack frame, when the key type holds a reference #4428 is in check_store_escape (src/hir/check/escape.rs:103). It records the provenance of the right side for any ident = rhs, and it does not check that the stored type can hold a reference. The let path at src/hir/check/stmt.rs:990 does check this. So h = map_hash(&key, ..) with h : u64 stores Local for h. The same type_can_hold_reference check on that path, with the cast probe fix, would fix check: E4005 says a u64 made from &key points into the stack frame, when the key type holds a reference #4428 without the check in ref_provenance_of and its regression.

  3. A cast to a type that the checker cannot find (src/hir/check/escape.rs:807). The probe now gives c.target_ty as the source writes it. A struct name that type_can_hold_reference cannot find gives false, for example an alias or Self. Then the value loses its local provenance, and the checker does not check it for escape. Before this PR, Unknown kept the check conservative.

Cost

  1. Each recursive call of ref_provenance_of now runs check_expr_type_probe and type_can_hold_reference, and both recurse too. So the work grows with the square of the depth of the expression. A probe of a call to a function that is not in env.functions also scans mono.functions, once for each argument in check_escape_in_calls.

  2. src/hir/check/escape.rs:314 runs the same probe again after ref_provenance_of(arg). The check is now in three places (here, stmt.rs:990 and stmt.rs:3189), and they can drift apart. Keep it at the callers or in ref_provenance_of, not both.

Tests

  1. The pass fixture covers two changes at once. The cast probe change alone stops map_tag(h) as u8 from counting at escape.rs:314. So the fixture probably stays green without the check in ref_provenance_of, and it cannot show that the PR needs this check. No fail fixture shows that E4005 is still reported where the new check removes provenance.

Process

  1. The fixture was not formatted with vx-format (lines 46, 73 and 79). So the "Check Vx Formatting" step of CI failed, and the release test suite did not run on this PR. The aarch64 job failed on an apt download error that is not related to this PR.
  2. The comments at access.rs:2421 and escape.rs:806, and the fixture header, contain the issue number. agents/AGENTS.md asks for comments with no issue IDs.
  3. The comment at access.rs:2417 has five lines for a three-line check, and most of it is history. The same goes for escape.rs:804-806.
  4. The commit message has no Fixes: #4428 line, so the merge does not close the issue.

The release test job failed on this file: vx-format rewrote the
one-line if and the two Piece literals. Debug tests do not run
that check.

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.

check: E4005 says a u64 made from &key points into the stack frame, when the key type holds a reference

2 participants