W1039

W1039 — ?n gives an extent the name of a constant or a const parameter. In the function, n is the extent, so the function cannot use the constant.

What the compiler reports

Warning[W1039] at {{.*}}:[[#@LINE+1]]:4: `?k` names an extent of `t`, so in this function `k` is that extent and not a constant with the name `k`
Warning[W1039] at {{.*}}:[[#@LINE+1]]:4: `?k` names an extent of `t`, so in this function `k` is that extent and not a constant with the name `k`
Warning[W1039] at {{.*}}:[[#@LINE+1]]:4: `?m` names an extent of `t`, so in this function `m` is that extent and not a const parameter with the name `m`
Warning[W1005] at {{.*}}:[[#@LINE+1]]:3: `t` is already a parameter of this function; this `let` hides it
Error[E8006] at {{.*}}:[[#@LINE+1]]:10: this call to 'quarter' may not meet its precondition `n % 4 == 0`
Error[E8006] at {{.*}}:[[#@LINE+1]]:10: this call to 'quarter' may not meet its precondition `n % 4 == 0`
Error[E8001] at {{.*}}:[[#@LINE+1]]:3: Function 'through_a_let' cannot prove postcondition (ensures) at compile time
Error[E8001] at {{.*}}:[[#@LINE+1]]:3: Function 'through_a_return' cannot prove postcondition (ensures) at compile time
Error[E8006] at {{.*}}:[[#@LINE+1]]:10: this call to 'quarter' may not meet its precondition `n % 4 == 0`
Error[E3069] at {{.*}}:[[#@LINE+1]]:3: `n` is the name of an extent of `t` (`?n`), so no variable of the body can have this name
Error[E3069] at {{.*}}:[[#@LINE+1]]:3: `n` is the name of an extent of `t` (`?n`), so no variable of the body can have this name
Error[E3069] at {{.*}}:[[#@LINE+1]]:3: `n` is an extent of `t` (`?n`), and nothing can change it
Error[E4001] at {{.*}}:[[#@LINE+1]]:10: `n` is an extent of `t`, and `t` was moved before this read. Read `n` into a variable before the move
Error[E8006] at {{.*}}:[[#@LINE+1]]:11: this call to 'quarter' may not meet its precondition `n % 4 == 0`
Error[E8006] at {{.*}}:[[#@LINE+1]]:11: this call to 'quarter' may not meet its precondition `n % 4 == 0`
Error[E8006] at {{.*}}:[[#@LINE+1]]:11: this call to 'quarter_of' may not meet its precondition `n % 4 == 0`
Error[E3068] at {{.*}}:[[#@LINE+1]]:11: this call to 'dot' gives `?n` the extents 6 and 8, but each `?n` of 'dot' is the same extent

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// `?n` in the tensor type of a parameter names that run-time extent. A call must show the
// `requires` about `n`, and it cannot give one name two different extents. The callee's `n` is
// the extent of the argument, never a name of the caller. The body cannot have a variable with
// the name `n` or change `n`, and it cannot read `n` after a move of the tensor. The compiler
// gives its warnings before its errors, so the functions that warn come first.
//
import tests::modules::a_function_that_names_an_extent;

const k : usize = 3;

fn hides(t : &Tensor<f32, [?k]>) -> usize {
  return k;
}

// The instance that `main` makes gives no second W1039.
fn hides_in_a_generic<T>(t : &Tensor<T, [?k]>) -> usize {
  return k;
}

fn hides_a_parameter<const m : usize>(t : &Tensor<f32, [?m]>) -> usize {
  return m;
}

fn quarter(t : &Tensor<f32, [?n]>) -> usize
requires n % 4 == 0
{
  return n / 4;
}

fn length(t : &Tensor<f32, [?n]>) -> usize
ensures return == n
{
  return n;
}

// After the `let`, `t` is `u`, whose extent the caller does not know.
fn shadows(t : &Tensor<f32, [?j]>, u : &Tensor<f32, [?]>) -> usize
requires j % 4 == 0
{
  let t = u;
  return quarter(t);
}

// The `n` of `quarter` is the extent of `u`, not the `n` of this function.
fn another_n(t : &Tensor<f32, [?n]>, u : &Tensor<f32, [?]>) -> usize
requires n % 4 == 0
{
  return quarter(u);
}

// `length(u)` is the extent of `u`, not the `n` of this function.
fn through_a_let(t : &Tensor<f32, [?n]>, u : &Tensor<f32, [?]>) -> usize
requires n == 8
ensures return == 8
{
  let r = length(u);
  return r;
}

fn through_a_return(t : &Tensor<f32, [?n]>, u : &Tensor<f32, [?]>) -> usize
requires n == 8
ensures return == 8
{
  return length(u);
}

fn dot(a : &Tensor<f32, [?n]>, b : &Tensor<f32, [?n]>) -> f32 {
  return a[0] * b[0];
}

// `m % 2 == 0` does not show `n % 4 == 0`.
fn only_even(t : &Tensor<f32, [?m]>) -> usize
requires m % 2 == 0
{
  return quarter(t);
}

fn binds(t : &Tensor<f32, [?n]>) -> usize {
  let n = 3;
  return n;
}

fn in_a_closure(t : &Tensor<f32, [?n]>) -> usize {
  let f = |n : usize| n + 1;
  return f(1);
}

fn assigns(t : &Tensor<f32, [?n]>) -> usize {
  n = 3;
  return n;
}

fn take(t : Tensor<f32, [?]>) -> usize {
  return t.len() as usize;
}

fn moved(t : Tensor<f32, [?n]>) -> usize {
  take(t);
  return n;
}

fn main() -> i32 {
  let six = Tensor<f32, [6]>::fill(1.0);
  let eight = Tensor<f32, [8]>::fill(1.0);
  let unknown = Tensor<f32, [?]>::new([8]);
  let a = quarter(&six);
  let b = quarter(&unknown);
  let c = quarter_of(&six);
  let d = dot(&six, &eight);
  let e = quarter(&eight) + quarter_of(&eight);
  let g = dot(&eight, &eight) + (hides_in_a_generic(&eight) as f32);
  println!("{} {} {} {} {} {}", a, b, c, d, e, g);
  return 0;
}

From tests/frontend/fail/calls_and_bodies_that_break_the_rules_of_extent_names.vx, which asserts this diagnostic on every commit.

Related