Skip to content

Commit dab7156

Browse files
committed
Auto merge of #117249 - matthiaskrgr:rollup-h4og5rv, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #116968 (Invalid `?` suggestion on mismatched `Ok(T)`) - #117032 (Enable cg_clif tests for riscv64gc) - #117106 (When expecting closure argument but finding block provide suggestion) - #117114 (Improve `stringify.rs` test) - #117188 (Avoid repeated interning of `env!("CFG_RELEASE")`) - #117243 (Explain implementation of mem::replace) r? `@ghost` `@rustbot` modify labels: rollup
2 parents aa1a71e + c0b1c1a commit dab7156

File tree

19 files changed

+784
-638
lines changed

19 files changed

+784
-638
lines changed

compiler/rustc_ast/src/ast.rs

+7
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ pub enum RangeSyntax {
734734
}
735735

736736
/// All the different flavors of pattern that Rust recognizes.
737+
//
738+
// Adding a new variant? Please update `test_pat` in `tests/ui/macros/stringify.rs`.
737739
#[derive(Clone, Encodable, Decodable, Debug)]
738740
pub enum PatKind {
739741
/// Represents a wildcard pattern (`_`).
@@ -967,6 +969,7 @@ impl Stmt {
967969
}
968970
}
969971

972+
// Adding a new variant? Please update `test_stmt` in `tests/ui/macros/stringify.rs`.
970973
#[derive(Clone, Encodable, Decodable, Debug)]
971974
pub enum StmtKind {
972975
/// A local (let) binding.
@@ -1345,6 +1348,7 @@ pub struct StructExpr {
13451348
pub rest: StructRest,
13461349
}
13471350

1351+
// Adding a new variant? Please update `test_expr` in `tests/ui/macros/stringify.rs`.
13481352
#[derive(Clone, Encodable, Decodable, Debug)]
13491353
pub enum ExprKind {
13501354
/// An array (`[a, b, c, d]`)
@@ -2015,6 +2019,8 @@ pub struct BareFnTy {
20152019
}
20162020

20172021
/// The various kinds of type recognized by the compiler.
2022+
//
2023+
// Adding a new variant? Please update `test_ty` in `tests/ui/macros/stringify.rs`.
20182024
#[derive(Clone, Encodable, Decodable, Debug)]
20192025
pub enum TyKind {
20202026
/// A variable-length slice (`[T]`).
@@ -2880,6 +2886,7 @@ pub struct ConstItem {
28802886
pub expr: Option<P<Expr>>,
28812887
}
28822888

2889+
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
28832890
#[derive(Clone, Encodable, Decodable, Debug)]
28842891
pub enum ItemKind {
28852892
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.

compiler/rustc_attr/src/builtin.rs

-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ pub const VERSION_PLACEHOLDER: &str = "CURRENT_RUSTC_VERSION";
2626

2727
pub const CURRENT_RUSTC_VERSION: &str = env!("CFG_RELEASE");
2828

29-
pub fn rust_version_symbol() -> Symbol {
30-
Symbol::intern(CURRENT_RUSTC_VERSION)
31-
}
32-
3329
pub fn is_builtin_attr(attr: &Attribute) -> bool {
3430
attr.is_doc_comment() || attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
3531
}

compiler/rustc_hir_typeck/src/demand.rs

+22-31
Original file line numberDiff line numberDiff line change
@@ -962,38 +962,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
962962
expected: Ty<'tcx>,
963963
found: Ty<'tcx>,
964964
) -> bool {
965-
let ty::Adt(e, args_e) = expected.kind() else {
966-
return false;
967-
};
968-
let ty::Adt(f, args_f) = found.kind() else {
969-
return false;
970-
};
971-
if e.did() != f.did() {
972-
return false;
973-
}
974-
if Some(e.did()) != self.tcx.get_diagnostic_item(sym::Result) {
975-
return false;
976-
}
977965
let map = self.tcx.hir();
978-
if let Some(hir::Node::Expr(expr)) = map.find_parent(expr.hir_id)
979-
&& let hir::ExprKind::Ret(_) = expr.kind
980-
{
981-
// `return foo;`
982-
} else if map.get_return_block(expr.hir_id).is_some() {
983-
// Function's tail expression.
984-
} else {
985-
return false;
986-
}
987-
let e = args_e.type_at(1);
988-
let f = args_f.type_at(1);
989-
if self
990-
.infcx
991-
.type_implements_trait(
992-
self.tcx.get_diagnostic_item(sym::Into).unwrap(),
993-
[f, e],
994-
self.param_env,
995-
)
996-
.must_apply_modulo_regions()
966+
let returned = matches!(
967+
map.find_parent(expr.hir_id),
968+
Some(hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Ret(_), .. }))
969+
) || map.get_return_block(expr.hir_id).is_some();
970+
if returned
971+
&& let ty::Adt(e, args_e) = expected.kind()
972+
&& let ty::Adt(f, args_f) = found.kind()
973+
&& e.did() == f.did()
974+
&& Some(e.did()) == self.tcx.get_diagnostic_item(sym::Result)
975+
&& let e_ok = args_e.type_at(0)
976+
&& let f_ok = args_f.type_at(0)
977+
&& self.infcx.can_eq(self.param_env, f_ok, e_ok)
978+
&& let e_err = args_e.type_at(1)
979+
&& let f_err = args_f.type_at(1)
980+
&& self
981+
.infcx
982+
.type_implements_trait(
983+
self.tcx.get_diagnostic_item(sym::Into).unwrap(),
984+
[f_err, e_err],
985+
self.param_env,
986+
)
987+
.must_apply_modulo_regions()
997988
{
998989
err.multipart_suggestion(
999990
"use `?` to coerce and return an appropriate `Err`, and wrap the resulting value \

compiler/rustc_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(never_type)]
55
#![feature(proc_macro_diagnostic)]
66
#![feature(proc_macro_span)]
7+
#![feature(proc_macro_tracked_env)]
78
#![allow(rustc::default_hash_types)]
89
#![deny(rustc::untranslatable_diagnostic)]
910
#![deny(rustc::diagnostic_outside_of_impl)]

compiler/rustc_macros/src/symbols.rs

+126-32
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use proc_macro2::{Span, TokenStream};
2626
use quote::quote;
2727
use std::collections::HashMap;
2828
use syn::parse::{Parse, ParseStream, Result};
29-
use syn::{braced, punctuated::Punctuated, Ident, LitStr, Token};
29+
use syn::{braced, punctuated::Punctuated, Expr, Ident, Lit, LitStr, Macro, Token};
3030

3131
#[cfg(test)]
3232
mod tests;
@@ -53,21 +53,46 @@ impl Parse for Keyword {
5353

5454
struct Symbol {
5555
name: Ident,
56-
value: Option<LitStr>,
56+
value: Value,
57+
}
58+
59+
enum Value {
60+
SameAsName,
61+
String(LitStr),
62+
Env(LitStr, Macro),
63+
Unsupported(Expr),
5764
}
5865

5966
impl Parse for Symbol {
6067
fn parse(input: ParseStream<'_>) -> Result<Self> {
6168
let name = input.parse()?;
62-
let value = match input.parse::<Token![:]>() {
63-
Ok(_) => Some(input.parse()?),
64-
Err(_) => None,
65-
};
69+
let colon_token: Option<Token![:]> = input.parse()?;
70+
let value = if colon_token.is_some() { input.parse()? } else { Value::SameAsName };
6671

6772
Ok(Symbol { name, value })
6873
}
6974
}
7075

76+
impl Parse for Value {
77+
fn parse(input: ParseStream<'_>) -> Result<Self> {
78+
let expr: Expr = input.parse()?;
79+
match &expr {
80+
Expr::Lit(expr) => {
81+
if let Lit::Str(lit) = &expr.lit {
82+
return Ok(Value::String(lit.clone()));
83+
}
84+
}
85+
Expr::Macro(expr) => {
86+
if expr.mac.path.is_ident("env") && let Ok(lit) = expr.mac.parse_body() {
87+
return Ok(Value::Env(lit, expr.mac.clone()));
88+
}
89+
}
90+
_ => {}
91+
}
92+
Ok(Value::Unsupported(expr))
93+
}
94+
}
95+
7196
struct Input {
7297
keywords: Punctuated<Keyword, Token![,]>,
7398
symbols: Punctuated<Symbol, Token![,]>,
@@ -111,6 +136,37 @@ pub fn symbols(input: TokenStream) -> TokenStream {
111136
output
112137
}
113138

139+
struct Preinterned {
140+
idx: u32,
141+
span_of_name: Span,
142+
}
143+
144+
struct Entries {
145+
map: HashMap<String, Preinterned>,
146+
}
147+
148+
impl Entries {
149+
fn with_capacity(capacity: usize) -> Self {
150+
Entries { map: HashMap::with_capacity(capacity) }
151+
}
152+
153+
fn insert(&mut self, span: Span, str: &str, errors: &mut Errors) -> u32 {
154+
if let Some(prev) = self.map.get(str) {
155+
errors.error(span, format!("Symbol `{str}` is duplicated"));
156+
errors.error(prev.span_of_name, "location of previous definition".to_string());
157+
prev.idx
158+
} else {
159+
let idx = self.len();
160+
self.map.insert(str.to_string(), Preinterned { idx, span_of_name: span });
161+
idx
162+
}
163+
}
164+
165+
fn len(&self) -> u32 {
166+
u32::try_from(self.map.len()).expect("way too many symbols")
167+
}
168+
}
169+
114170
fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
115171
let mut errors = Errors::default();
116172

@@ -127,20 +183,9 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
127183
let mut keyword_stream = quote! {};
128184
let mut symbols_stream = quote! {};
129185
let mut prefill_stream = quote! {};
130-
let mut counter = 0u32;
131-
let mut keys =
132-
HashMap::<String, Span>::with_capacity(input.keywords.len() + input.symbols.len() + 10);
186+
let mut entries = Entries::with_capacity(input.keywords.len() + input.symbols.len() + 10);
133187
let mut prev_key: Option<(Span, String)> = None;
134188

135-
let mut check_dup = |span: Span, str: &str, errors: &mut Errors| {
136-
if let Some(prev_span) = keys.get(str) {
137-
errors.error(span, format!("Symbol `{str}` is duplicated"));
138-
errors.error(*prev_span, "location of previous definition".to_string());
139-
} else {
140-
keys.insert(str.to_string(), span);
141-
}
142-
};
143-
144189
let mut check_order = |span: Span, str: &str, errors: &mut Errors| {
145190
if let Some((prev_span, ref prev_str)) = prev_key {
146191
if str < prev_str {
@@ -156,49 +201,98 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
156201
let name = &keyword.name;
157202
let value = &keyword.value;
158203
let value_string = value.value();
159-
check_dup(keyword.name.span(), &value_string, &mut errors);
204+
let idx = entries.insert(keyword.name.span(), &value_string, &mut errors);
160205
prefill_stream.extend(quote! {
161206
#value,
162207
});
163208
keyword_stream.extend(quote! {
164-
pub const #name: Symbol = Symbol::new(#counter);
209+
pub const #name: Symbol = Symbol::new(#idx);
165210
});
166-
counter += 1;
167211
}
168212

169213
// Generate the listed symbols.
170214
for symbol in input.symbols.iter() {
171215
let name = &symbol.name;
216+
check_order(symbol.name.span(), &name.to_string(), &mut errors);
217+
172218
let value = match &symbol.value {
173-
Some(value) => value.value(),
174-
None => name.to_string(),
219+
Value::SameAsName => name.to_string(),
220+
Value::String(lit) => lit.value(),
221+
Value::Env(..) => continue, // in another loop below
222+
Value::Unsupported(expr) => {
223+
errors.list.push(syn::Error::new_spanned(
224+
expr,
225+
concat!(
226+
"unsupported expression for symbol value; implement support for this in ",
227+
file!(),
228+
),
229+
));
230+
continue;
231+
}
175232
};
176-
check_dup(symbol.name.span(), &value, &mut errors);
177-
check_order(symbol.name.span(), &name.to_string(), &mut errors);
233+
let idx = entries.insert(symbol.name.span(), &value, &mut errors);
178234

179235
prefill_stream.extend(quote! {
180236
#value,
181237
});
182238
symbols_stream.extend(quote! {
183-
pub const #name: Symbol = Symbol::new(#counter);
239+
pub const #name: Symbol = Symbol::new(#idx);
184240
});
185-
counter += 1;
186241
}
187242

188243
// Generate symbols for the strings "0", "1", ..., "9".
189-
let digits_base = counter;
190-
counter += 10;
191244
for n in 0..10 {
192245
let n = n.to_string();
193-
check_dup(Span::call_site(), &n, &mut errors);
246+
entries.insert(Span::call_site(), &n, &mut errors);
194247
prefill_stream.extend(quote! {
195248
#n,
196249
});
197250
}
198251

252+
// Symbols whose value comes from an environment variable. It's allowed for
253+
// these to have the same value as another symbol.
254+
for symbol in &input.symbols {
255+
let (env_var, expr) = match &symbol.value {
256+
Value::Env(lit, expr) => (lit, expr),
257+
Value::SameAsName | Value::String(_) | Value::Unsupported(_) => continue,
258+
};
259+
260+
if !proc_macro::is_available() {
261+
errors.error(
262+
Span::call_site(),
263+
"proc_macro::tracked_env is not available in unit test".to_owned(),
264+
);
265+
break;
266+
}
267+
268+
let value = match proc_macro::tracked_env::var(env_var.value()) {
269+
Ok(value) => value,
270+
Err(err) => {
271+
errors.list.push(syn::Error::new_spanned(expr, err));
272+
continue;
273+
}
274+
};
275+
276+
let idx = if let Some(prev) = entries.map.get(&value) {
277+
prev.idx
278+
} else {
279+
prefill_stream.extend(quote! {
280+
#value,
281+
});
282+
entries.insert(symbol.name.span(), &value, &mut errors)
283+
};
284+
285+
let name = &symbol.name;
286+
symbols_stream.extend(quote! {
287+
pub const #name: Symbol = Symbol::new(#idx);
288+
});
289+
}
290+
291+
let symbol_digits_base = entries.map["0"].idx;
292+
let preinterned_symbols_count = entries.len();
199293
let output = quote! {
200-
const SYMBOL_DIGITS_BASE: u32 = #digits_base;
201-
const PREINTERNED_SYMBOLS_COUNT: u32 = #counter;
294+
const SYMBOL_DIGITS_BASE: u32 = #symbol_digits_base;
295+
const PREINTERNED_SYMBOLS_COUNT: u32 = #preinterned_symbols_count;
202296

203297
#[doc(hidden)]
204298
#[allow(non_upper_case_globals)]

compiler/rustc_macros/src/symbols/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn test_symbols() {
2727

2828
let body_tokens = m.mac.tokens.clone();
2929

30-
test_symbols_macro(body_tokens, &[]);
30+
test_symbols_macro(body_tokens, &["proc_macro::tracked_env is not available in unit test"]);
3131
}
3232

3333
fn test_symbols_macro(input: TokenStream, expected_errors: &[&str]) {

compiler/rustc_passes/src/lib_features.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! collect them instead.
66
77
use rustc_ast::Attribute;
8-
use rustc_attr::{rust_version_symbol, VERSION_PLACEHOLDER};
8+
use rustc_attr::VERSION_PLACEHOLDER;
99
use rustc_hir::intravisit::Visitor;
1010
use rustc_middle::hir::nested_filter;
1111
use rustc_middle::middle::lib_features::LibFeatures;
@@ -59,7 +59,7 @@ impl<'tcx> LibFeatureCollector<'tcx> {
5959
if let Some(s) = since
6060
&& s.as_str() == VERSION_PLACEHOLDER
6161
{
62-
since = Some(rust_version_symbol());
62+
since = Some(sym::env_CFG_RELEASE);
6363
}
6464

6565
if let Some(feature) = feature {

0 commit comments

Comments
 (0)