E3074
E3074 — A call ofString::from_literalwhose argument is not a string literal. The argument can also be aletwithoutmutwhose value is a string literal. The call needs nounsafebecause the compiler makes each literal, so it knows that the bytes are a C string. It does not know that for another pointer. For such a pointer, callString::from_c_strinunsafecode.
What the compiler reports
Error[E3074] at {{.*}}:[[#@LINE+1]]:10: the argument of `String::from_literal` must be a string literal, or a `let` without `mut` whose value is a string literal. For another C string, call `String::from_c_str` in `unsafe` code
Error[E3074] at {{.*}}:[[#@LINE+1]]:10: the argument of `String::from_literal`
Error[E3074] at {{.*}}:[[#@LINE+1]]:10: the argument of `String::from_literal`
Error[E3074] at {{.*}}:[[#@LINE+1]]:12: the argument of `String::from_literal`
Error[E3074] at {{.*}}:[[#@LINE+1]]:10: the argument of `String::from_literal`
Error[E3074] at {{.*}}:[[#@LINE+1]]:10: the argument of `String::from_literal`
Error[E3074] at {{.*}}:[[#@LINE+1]]:19: the argument of `String::from_literal`
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
//
// `String::from_literal` needs no `unsafe`, because the compiler knows that a string literal is
// a C string. So its argument must be a string literal, or a `let` without `mut` whose value is
// a string literal. Any other pointer is an error (E3074), also in `unsafe` code.
//
import std::string;
fn from_a_parameter(text : *const i8) -> String {
return String::from_literal(text);
}
// A `let mut` can get another pointer later, so it is not a name for a literal.
fn from_a_let_mut() -> String {
let mut text = "first";
text = "second";
return String::from_literal(text);
}
// The second `let` gives the name `text` to a pointer that is not a literal.
fn from_a_name_that_a_later_let_took(other : *const i8) -> String {
let text = "a literal";
let text = other;
return String::from_literal(text);
}
// Inside the `if`, `text` is the parameter `other`. After it, `text` is the literal again.
fn from_a_name_that_a_block_took(other : *const i8, inner : bool) -> String {
let text = "a literal";
if inner {
let text = other;
return String::from_literal(text);
}
return String::from_literal(text);
}
fn from_a_cast() -> String {
return String::from_literal("a literal" as *const i8);
}
fn from_the_pointer_of_a_string(s : &String) -> String {
return String::from_literal(s.as_c_str());
}
fn in_unsafe_code(text : *const i8) -> String {
return unsafe { String::from_literal(text) };
}
fn main() -> i32 {
let name = "a name";
let a = String::from_literal("a literal");
let b = String::from_literal(name);
let c = from_a_parameter(name);
let d = from_a_let_mut();
let e = from_a_name_that_a_later_let_took(name);
let f = from_a_name_that_a_block_took(name, true);
let g = from_a_cast();
let h = from_the_pointer_of_a_string(&a);
let i = in_unsafe_code(name);
println!("{} {} {} {} {} {} {} {}", b, c, d, e, f, g, h, i);
return 0;
}
From tests/frontend/fail/string_from_literal_takes_only_a_string_literal.vx, which asserts this diagnostic on every commit.
Related
A tour of VxThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
E3073A C function with a struct parameter or result, used as a value, as in
let f = c_function;. C passes a struct in other registers than Vx does, and the type of the value does not say which of the two the function is. So a call through the value would pass the struct the wrong way. Call the C function by its name.