Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 9 pull requests #131723

Merged
merged 20 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e904761
Use environment variables instead of command line arguments for merge…
GuillaumeGomez Oct 1, 2024
2bd0d07
Expand set_ptr_value / with_metadata_of docs
HeroicKatora Oct 6, 2024
c128b4c
Fix typo thing->thin referring to pointer
HeroicKatora Oct 13, 2024
feecfaa
Fix bug where `option_env!` would return `None` when env var is prese…
beetrees Mar 18, 2024
b6b6c12
Update lint message for ABI not supported
tdittr Oct 14, 2024
c6e1fbf
Fix up-to-date checking for run-make tests
Zalathar Oct 14, 2024
7500e09
Move trait bound modifiers into hir::PolyTraitRef
compiler-errors Oct 13, 2024
95dba28
Move trait bound modifiers into ast::PolyTraitRef
compiler-errors Oct 13, 2024
c3b696d
Suppress import errors for traits that couldve applied in method look…
compiler-errors Oct 14, 2024
eb6062c
Resolved python deprecation warning in publish_toolstate.py
alex Oct 14, 2024
dda3066
Remove `'apostrophes'` from `rustc_parse_format`
ShE3py Oct 14, 2024
6d99996
Rollup merge of #122670 - beetrees:non-unicode-option-env-error, r=co…
matthiaskrgr Oct 15, 2024
bb2f970
Rollup merge of #131095 - GuillaumeGomez:switch-to-env-variables, r=n…
matthiaskrgr Oct 15, 2024
09103f2
Rollup merge of #131339 - HeroicKatora:set_ptr_value-documentation, r…
matthiaskrgr Oct 15, 2024
4d53a28
Rollup merge of #131652 - compiler-errors:modifiers, r=Nadrieril,jiey…
matthiaskrgr Oct 15, 2024
bd649b4
Rollup merge of #131675 - tdittr:update-unsupported-abi-message, r=co…
matthiaskrgr Oct 15, 2024
258c177
Rollup merge of #131681 - Zalathar:fix-run-make-stamp, r=jieyouxu
matthiaskrgr Oct 15, 2024
2e2c433
Rollup merge of #131702 - compiler-errors:method-lookup-trait-warning…
matthiaskrgr Oct 15, 2024
d82a49d
Rollup merge of #131703 - alex:patch-1, r=Kobzol
matthiaskrgr Oct 15, 2024
c99c4d4
Rollup merge of #131710 - ShE3py:parse_format_apostrophes, r=compiler…
matthiaskrgr Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions compiler/rustc_builtin_macros/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_span::symbol::{Ident, Symbol, kw, sym};
use thin_vec::thin_vec;

use crate::errors;
use crate::util::{expr_to_string, get_exprs_from_tts, get_single_str_from_tts};
use crate::util::{expr_to_string, get_exprs_from_tts, get_single_expr_from_tts};

fn lookup_env<'cx>(cx: &'cx ExtCtxt<'_>, var: Symbol) -> Result<Symbol, VarError> {
let var = var.as_str();
Expand All @@ -32,19 +32,28 @@ pub(crate) fn expand_option_env<'cx>(
sp: Span,
tts: TokenStream,
) -> MacroExpanderResult<'cx> {
let ExpandResult::Ready(mac) = get_single_str_from_tts(cx, sp, tts, "option_env!") else {
let ExpandResult::Ready(mac_expr) = get_single_expr_from_tts(cx, sp, tts, "option_env!") else {
return ExpandResult::Retry(());
};
let var_expr = match mac_expr {
Ok(var_expr) => var_expr,
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
};
let ExpandResult::Ready(mac) =
expr_to_string(cx, var_expr.clone(), "argument must be a string literal")
else {
return ExpandResult::Retry(());
};
let var = match mac {
Ok(var) => var,
Ok((var, _)) => var,
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
};

let sp = cx.with_def_site_ctxt(sp);
let value = lookup_env(cx, var).ok();
cx.sess.psess.env_depinfo.borrow_mut().insert((var, value));
let value = lookup_env(cx, var);
cx.sess.psess.env_depinfo.borrow_mut().insert((var, value.as_ref().ok().copied()));
let e = match value {
None => {
Err(VarError::NotPresent) => {
let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
cx.expr_path(cx.path_all(
sp,
Expand All @@ -58,7 +67,18 @@ pub(crate) fn expand_option_env<'cx>(
))],
))
}
Some(value) => {
Err(VarError::NotUnicode(_)) => {
let ExprKind::Lit(token::Lit {
kind: LitKind::Str | LitKind::StrRaw(..), symbol, ..
}) = &var_expr.kind
else {
unreachable!("`expr_to_string` ensures this is a string lit")
};

let guar = cx.dcx().emit_err(errors::EnvNotUnicode { span: sp, var: *symbol });
return ExpandResult::Ready(DummyResult::any(sp, guar));
}
Ok(value) => {
cx.expr_call_global(sp, cx.std_path(&[sym::option, sym::Option, sym::Some]), thin_vec![
cx.expr_str(sp, value)
])
Expand Down
32 changes: 25 additions & 7 deletions compiler/rustc_builtin_macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ pub(crate) fn get_single_str_spanned_from_tts(
tts: TokenStream,
name: &str,
) -> ExpandResult<Result<(Symbol, Span), ErrorGuaranteed>, ()> {
let ExpandResult::Ready(ret) = get_single_expr_from_tts(cx, span, tts, name) else {
return ExpandResult::Retry(());
};
let ret = match ret {
Ok(ret) => ret,
Err(e) => return ExpandResult::Ready(Err(e)),
};
expr_to_spanned_string(cx, ret, "argument must be a string literal").map(|res| {
res.map_err(|err| match err {
Ok((err, _)) => err.emit(),
Err(guar) => guar,
})
.map(|(symbol, _style, span)| (symbol, span))
})
}

/// Interpreting `tts` as a comma-separated sequence of expressions,
/// expect exactly one expression, or emit an error and return `Err`.
pub(crate) fn get_single_expr_from_tts(
cx: &mut ExtCtxt<'_>,
span: Span,
tts: TokenStream,
name: &str,
) -> ExpandResult<Result<P<ast::Expr>, ErrorGuaranteed>, ()> {
let mut p = cx.new_parser_from_tts(tts);
if p.token == token::Eof {
let guar = cx.dcx().emit_err(errors::OnlyOneArgument { span, name });
Expand All @@ -185,13 +209,7 @@ pub(crate) fn get_single_str_spanned_from_tts(
if p.token != token::Eof {
cx.dcx().emit_err(errors::OnlyOneArgument { span, name });
}
expr_to_spanned_string(cx, ret, "argument must be a string literal").map(|res| {
res.map_err(|err| match err {
Ok((err, _)) => err.emit(),
Err(guar) => guar,
})
.map(|(symbol, _style, span)| (symbol, span))
})
ExpandResult::Ready(Ok(ret))
}

/// Extracts comma-separated expressions from `tts`.
Expand Down
18 changes: 10 additions & 8 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,17 +1107,19 @@ pub(crate) mod builtin {
///
/// If the named environment variable is present at compile time, this will
/// expand into an expression of type `Option<&'static str>` whose value is
/// `Some` of the value of the environment variable. If the environment
/// variable is not present, then this will expand to `None`. See
/// [`Option<T>`][Option] for more information on this type. Use
/// [`std::env::var`] instead if you want to read the value at runtime.
/// `Some` of the value of the environment variable (a compilation error
/// will be emitted if the environment variable is not a valid Unicode
/// string). If the environment variable is not present, then this will
/// expand to `None`. See [`Option<T>`][Option] for more information on this
/// type. Use [`std::env::var`] instead if you want to read the value at
/// runtime.
///
/// [`std::env::var`]: ../std/env/fn.var.html
///
/// A compile time error is never emitted when using this macro regardless
/// of whether the environment variable is present or not.
/// To emit a compile error if the environment variable is not present,
/// use the [`env!`] macro instead.
/// A compile time error is only emitted when using this macro if the
/// environment variable exists and is not a valid Unicode string. To also
/// emit a compile error if the environment variable is not present, use the
/// [`env!`] macro instead.
///
/// # Examples
///
Expand Down
1 change: 1 addition & 0 deletions tests/run-make/non-unicode-env/non_unicode_env.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
let _ = env!("NON_UNICODE_VAR");
let _ = option_env!("NON_UNICODE_VAR");
}
10 changes: 9 additions & 1 deletion tests/run-make/non-unicode-env/non_unicode_env.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ error: environment variable `NON_UNICODE_VAR` is not a valid Unicode string
|
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error
error: environment variable `NON_UNICODE_VAR` is not a valid Unicode string
--> non_unicode_env.rs:3:13
|
3 | let _ = option_env!("NON_UNICODE_VAR");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `option_env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors