diesel-rs/diesel · error
Expected ` ` to be a u16 integer value
Error message
Expected `{}` to be a u16 integer value What it means
After reading the environment variable in `diesel_for_each_tuple!`, the macro parses its value as a `u16`. If the string cannot be parsed as a u16 (e.g. non-numeric, negative, or too large), the macro emits this compile error.
Solutions
- Set the variable to a plain decimal integer within 0–65535 (e.g. `export MAX_TUPLE_SIZE=32`).
- Trim surrounding whitespace/quotes from the exported value.
- Use a literal integer argument in the macro instead of `env!`.
Example fix
// before (shell) export MAX_TUPLE_SIZE="32 tuples" // after (shell) export MAX_TUPLE_SIZE=32
Defensive patterns
Strategy: validation
Validate before calling
// Validate before export / before build
python3 -c 'import os,sys; v=os.environ.get("MAX_TUPLE_SIZE",""); n=int(v) if v.isdigit() else -1; sys.exit(0 if 0<=n<=65535 else 1)' || { echo "MAX_TUPLE_SIZE must be a u16 integer"; exit 1; } Prevention
- Export values unquoted and without units or whitespace
- Keep tuple sizes <= 65535
- Add a lint step in CI that validates numeric env vars
When it happens
Trigger: `env!(VAR)` where the variable contains something like `"three"`, `"-1"`, `"70000"`, or has stray whitespace.
Common situations: Copy-pasting a value with a trailing space or newline; exporting a string with units (`"16 tuples"`); a number exceeding u16 range for huge tuple sizes.
Understand the failure class
Background: "is not a valid" / "Invalid ... value" environment variable errors: how libraries validate env vars and what to do when they reject yours — this error's family across 48 libraries.
Related errors
- Expected ` ` to be set as environment variable
- expected attribute `name` help: the correct format looks…
- unknown attribute, expected
- unexpected end of input, expected `=` help: the correct…
- expected type
AI-assisted analysis of diesel-rs/diesel@6fa6ed01b2 (2026-09-07).
Data as JSON: /api/errors/590b4c3e6f21a908.
Report an issue: GitHub.
Appendix: source
Thrown at diesel_derives/src/diesel_for_each_tuple.rs:78
return Err(syn::Error::new(
macro_ident.span(),
"only the `env!` macro is expected here",
));
}
let _bang = input.parse::<syn::Token![!]>()?;
let name;
syn::parenthesized!(name in input);
let s = name.parse::<syn::LitStr>()?;
std::env::var(s.value())
.map_err(|_| {
syn::Error::new(
s.span(),
format!("Expected `{}` to be set as environment variable", s.value()),
)
})?
.parse::<u16>()
.map_err(|_| {
syn::Error::new(
s.span(),
format!("Expected `{}` to be a u16 integer value", s.value()),
)
})?
} else {
input.parse::<syn::LitInt>()?.base10_parse()?
};
Ok(Self { inner, max_size })
}
}
View on GitHub (pinned to 6fa6ed01b2)