W1038

W1038 — A tasks block that can fail, whose value nothing uses. When the block fails, the code after it goes on, and nothing sees the error.

What the compiler reports

Warning[W1038] at {{.*}}.vx:[[@LINE+3]]:3: this `tasks` block can fail, but nothing uses its value, a 'Result<i32, i32>'. When the block fails, the code after it goes on, and nothing sees the error. Use the value, as in `let r = tasks { .. };`
Warning[W1038] at {{.*}}.vx:[[@LINE+3]]:3: this `tasks` block can fail
Warning[W1038] at {{.*}}.vx:[[@LINE+4]]:5: this `tasks` block can fail
{{^}}failed_join 5 0{{$}}
{{^}}question_mark 5 0{{$}}
{{^}}in_an_if 5 0{{$}}
{{^}}used 5 -7{{$}}
{{^}}underscore 5 0{{$}}
{{^}}cannot_fail 2{{$}}

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
//
// A `tasks` block that can fail, whose value nothing uses, gets a warning. When it fails, the code
// after it goes on. A block whose value a `let` takes, also `let _`, gets no warning, and neither
// does a block that cannot fail.
//

import core::result;

fn half(x : i32) -> Result<i32, i32> {
  if x % 2 == 0 {
    return Result<i32, i32>::Ok(x / 2);
  }
  return Result<i32, i32>::Err(x);
}

fn failed_join(x : i32) -> i32 {
  let mut seen = 0;
  tasks {
    let h = spawn {
      Result<i32, i32>::Ok(half(x)? + 1)
    };
    seen = h.join();
    Result<i32, i32>::Ok(seen)
  };
  return seen;
}

fn question_mark(x : i32) -> i32 {
  let mut seen = 0;
  tasks {
    seen = half(x)? + 1;
    Result<i32, i32>::Ok(seen)
  };
  return seen;
}

fn in_an_if(x : i32, go : bool) -> i32 {
  let mut seen = 0;
  if go {
    tasks {
      seen = half(x)? + 1;
      Result<i32, i32>::Ok(seen)
    }
  }
  return seen;
}

fn used(x : i32) -> i32 {
  let r = tasks {
    let h = spawn {
      Result<i32, i32>::Ok(half(x)? + 1)
    };
    let v = h.join();
    Result<i32, i32>::Ok(v)
  };
  match r {
    Result<i32, i32>::Ok(v) => {
      return v;
    }
    Result<i32, i32>::Err(e) => {
      return -e;
    }
  }
}

fn underscore(x : i32) -> i32 {
  let mut seen = 0;
  let _ = tasks {
    let h = spawn {
      Result<i32, i32>::Ok(half(x)? + 1)
    };
    seen = h.join();
    Result<i32, i32>::Ok(seen)
  };
  return seen;
}

fn cannot_fail(x : i32) -> i32 {
  let mut seen = 0;
  tasks {
    let h = spawn {
      x + 1
    };
    seen = h.join();
    seen
  };
  return seen;
}

fn main() -> i32 {
  println!("failed_join {} {}", failed_join(8), failed_join(7));
  println!("question_mark {} {}", question_mark(8), question_mark(7));
  println!("in_an_if {} {}", in_an_if(8, true), in_an_if(7, true));
  println!("used {} {}", used(8), used(7));
  println!("underscore {} {}", underscore(8), underscore(7));
  println!("cannot_fail {}", cannot_fail(1));
  return 0;
}

From tests/backend/pass/a_tasks_block_whose_value_nothing_uses_gets_a_warning.vx, which asserts this diagnostic on every commit.

Related