Skip to content

Commit c915789

Browse files
committed
Auto merge of #153284 - JonathanBrouwer:rollup-oug6DDR, r=JonathanBrouwer
Rollup of 5 pull requests Successful merges: - #151962 (Fix next-solver ICE on PointeeSized goals) - #153161 (Rejig `rustc_with_all_queries!`) - #152164 (Lint unused features) - #153191 ( don't emit `unused_results` lint for tuples of "trivial" types ) - #153273 (vec/mod.rs: add missing period in "ie." in docs)
2 parents 8ddf4ef + 299e863 commit c915789

119 files changed

Lines changed: 655 additions & 261 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,11 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
338338
}
339339
}
340340

341-
Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
341+
let linker = Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend);
342+
343+
tcx.report_unused_features();
344+
345+
Some(linker)
342346
});
343347

344348
// Linking is done outside the `compiler.enter()` so that the

compiler/rustc_errors/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
#![allow(rustc::direct_use_of_rustc_type_ir)]
88
#![cfg_attr(bootstrap, feature(assert_matches))]
99
#![feature(associated_type_defaults)]
10-
#![feature(box_patterns)]
1110
#![feature(default_field_values)]
12-
#![feature(error_reporter)]
1311
#![feature(macro_metavar_expr_concat)]
1412
#![feature(negative_impls)]
1513
#![feature(never_type)]

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
13301330
safety: AttributeSafety::Normal,
13311331
template: template!(NameValueStr: "name"),
13321332
duplicates: ErrorFollowing,
1333-
gate: Gated{
1333+
gate: Gated {
13341334
feature: sym::rustc_attrs,
13351335
message: "use of an internal attribute",
13361336
check: Features::rustc_attrs,

compiler/rustc_feature/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,5 @@ pub use builtin_attrs::{
137137
pub use removed::REMOVED_LANG_FEATURES;
138138
pub use unstable::{
139139
DEPENDENT_FEATURES, EnabledLangFeature, EnabledLibFeature, Features, INCOMPATIBLE_FEATURES,
140-
UNSTABLE_LANG_FEATURES,
140+
TRACK_FEATURE, UNSTABLE_LANG_FEATURES,
141141
};

compiler/rustc_feature/src/unstable.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33
use std::path::PathBuf;
44
use std::time::{SystemTime, UNIX_EPOCH};
55

6+
use rustc_data_structures::AtomicRef;
67
use rustc_data_structures::fx::FxHashSet;
78
use rustc_span::{Span, Symbol, sym};
89

910
use super::{Feature, to_nonzero};
1011

12+
fn default_track_feature(_: Symbol) {}
13+
14+
/// Recording used features in the dependency graph so incremental can
15+
/// replay used features when needed.
16+
pub static TRACK_FEATURE: AtomicRef<fn(Symbol)> = AtomicRef::new(&(default_track_feature as _));
17+
1118
#[derive(PartialEq)]
1219
enum FeatureStatus {
1320
Default,
@@ -103,7 +110,12 @@ impl Features {
103110

104111
/// Is the given feature enabled (via `#[feature(...)]`)?
105112
pub fn enabled(&self, feature: Symbol) -> bool {
106-
self.enabled_features.contains(&feature)
113+
if self.enabled_features.contains(&feature) {
114+
TRACK_FEATURE(feature);
115+
true
116+
} else {
117+
false
118+
}
107119
}
108120
}
109121

@@ -124,7 +136,7 @@ macro_rules! declare_features {
124136
impl Features {
125137
$(
126138
pub fn $feature(&self) -> bool {
127-
self.enabled_features.contains(&sym::$feature)
139+
self.enabled(sym::$feature)
128140
}
129141
)*
130142

compiler/rustc_interface/src/callbacks.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use std::fmt;
1313

1414
use rustc_errors::DiagInner;
15-
use rustc_middle::dep_graph::TaskDepsRef;
15+
use rustc_middle::dep_graph::{DepNodeIndex, QuerySideEffect, TaskDepsRef};
1616
use rustc_middle::ty::tls;
17+
use rustc_span::Symbol;
1718

1819
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
1920
tls::with_context_opt(|icx| {
@@ -51,6 +52,25 @@ fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R)
5152
})
5253
}
5354

55+
fn track_feature(feature: Symbol) {
56+
tls::with_context_opt(|icx| {
57+
let Some(icx) = icx else {
58+
return;
59+
};
60+
let tcx = icx.tcx;
61+
62+
if let Some(dep_node_index) = tcx.sess.used_features.lock().get(&feature).copied() {
63+
tcx.dep_graph.read_index(DepNodeIndex::from_u32(dep_node_index));
64+
} else {
65+
let dep_node_index = tcx
66+
.dep_graph
67+
.encode_side_effect(tcx, QuerySideEffect::CheckFeature { symbol: feature });
68+
tcx.sess.used_features.lock().insert(feature, dep_node_index.as_u32());
69+
tcx.dep_graph.read_index(dep_node_index);
70+
}
71+
})
72+
}
73+
5474
/// This is a callback from `rustc_hir` as it cannot access the implicit state
5575
/// in `rustc_middle` otherwise.
5676
fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -70,4 +90,5 @@ pub fn setup_callbacks() {
7090
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
7191
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
7292
rustc_errors::TRACK_DIAGNOSTIC.swap(&(track_diagnostic as _));
93+
rustc_feature::TRACK_FEATURE.swap(&(track_feature as _));
7394
}

compiler/rustc_lint/src/unused/must_use.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ impl IsTyMustUse {
108108
_ => self,
109109
}
110110
}
111-
112-
fn yes(self) -> Option<MustUsePath> {
113-
match self {
114-
Self::Yes(must_use_path) => Some(must_use_path),
115-
_ => None,
116-
}
117-
}
118111
}
119112

120113
/// A path through a type to a `must_use` source. Contains useful info for the lint.
@@ -254,16 +247,23 @@ pub fn is_ty_must_use<'tcx>(
254247
// Default to `expr`.
255248
let elem_exprs = elem_exprs.iter().chain(iter::repeat(expr));
256249

257-
let nested_must_use = tys
258-
.iter()
259-
.zip(elem_exprs)
260-
.enumerate()
261-
.filter_map(|(i, (ty, expr))| {
262-
is_ty_must_use(cx, ty, expr, simplify_uninhabited).yes().map(|path| (i, path))
263-
})
264-
.collect::<Vec<_>>();
250+
let mut all_trivial = true;
251+
let mut nested_must_use = Vec::new();
252+
253+
tys.iter().zip(elem_exprs).enumerate().for_each(|(i, (ty, expr))| {
254+
let must_use = is_ty_must_use(cx, ty, expr, simplify_uninhabited);
255+
256+
all_trivial &= matches!(must_use, IsTyMustUse::Trivial);
257+
if let IsTyMustUse::Yes(path) = must_use {
258+
nested_must_use.push((i, path));
259+
}
260+
});
265261

266-
if !nested_must_use.is_empty() {
262+
if all_trivial {
263+
// If all tuple elements are trivial, mark the whole tuple as such.
264+
// i.e. don't emit `unused_results` for types such as `((), ())`
265+
IsTyMustUse::Trivial
266+
} else if !nested_must_use.is_empty() {
267267
IsTyMustUse::Yes(MustUsePath::TupleElement(nested_must_use))
268268
} else {
269269
IsTyMustUse::No

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,11 +1088,6 @@ declare_lint! {
10881088
/// crate-level [`feature` attributes].
10891089
///
10901090
/// [`feature` attributes]: https://doc.rust-lang.org/nightly/unstable-book/
1091-
///
1092-
/// Note: This lint is currently not functional, see [issue #44232] for
1093-
/// more details.
1094-
///
1095-
/// [issue #44232]: https://github.com/rust-lang/rust/issues/44232
10961091
pub UNUSED_FEATURES,
10971092
Warn,
10981093
"unused features found in crate-level `#[feature]` directives"

compiler/rustc_macros/src/query.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use syn::{
1010
};
1111

1212
mod kw {
13+
syn::custom_keyword!(non_query);
1314
syn::custom_keyword!(query);
1415
}
1516

@@ -54,12 +55,37 @@ struct Query {
5455
modifiers: QueryModifiers,
5556
}
5657

57-
impl Parse for Query {
58+
/// Declaration of a non-query dep kind.
59+
/// ```ignore (illustrative)
60+
/// /// Doc comment for `MyNonQuery`.
61+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doc_comments
62+
/// non_query MyNonQuery
63+
/// // ^^^^^^^^^^ name
64+
/// ```
65+
struct NonQuery {
66+
doc_comments: Vec<Attribute>,
67+
name: Ident,
68+
}
69+
70+
enum QueryEntry {
71+
Query(Query),
72+
NonQuery(NonQuery),
73+
}
74+
75+
impl Parse for QueryEntry {
5876
fn parse(input: ParseStream<'_>) -> Result<Self> {
5977
let mut doc_comments = check_attributes(input.call(Attribute::parse_outer)?)?;
6078

79+
// Try the non-query case first.
80+
if input.parse::<kw::non_query>().is_ok() {
81+
let name: Ident = input.parse()?;
82+
return Ok(QueryEntry::NonQuery(NonQuery { doc_comments, name }));
83+
}
84+
6185
// Parse the query declaration. Like `query type_of(key: DefId) -> Ty<'tcx>`
62-
input.parse::<kw::query>()?;
86+
if input.parse::<kw::query>().is_err() {
87+
return Err(input.error("expected `query` or `non_query`"));
88+
}
6389
let name: Ident = input.parse()?;
6490

6591
// `(key: DefId)`
@@ -84,7 +110,7 @@ impl Parse for Query {
84110
doc_comments.push(doc_comment_from_desc(&modifiers.desc.expr_list)?);
85111
}
86112

87-
Ok(Query { doc_comments, modifiers, name, key_pat, key_ty, return_ty })
113+
Ok(QueryEntry::Query(Query { doc_comments, modifiers, name, key_pat, key_ty, return_ty }))
88114
}
89115
}
90116

@@ -375,9 +401,9 @@ fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::Toke
375401
// macro producing a higher order macro that has all its token in the macro declaration we lose
376402
// any meaningful spans, resulting in rust-analyzer being unable to make the connection between
377403
// the query name and the corresponding providers field. The trick to fix this is to have
378-
// `rustc_queries` emit a field access with the given name's span which allows it to successfully
379-
// show references / go to definition to the corresponding provider assignment which is usually
380-
// the more interesting place.
404+
// `rustc_queries` emit a field access with the given name's span which allows it to
405+
// successfully show references / go to definition to the corresponding provider assignment
406+
// which is usually the more interesting place.
381407
let ra_hint = quote! {
382408
let crate::query::Providers { #name: _, .. };
383409
};
@@ -393,9 +419,10 @@ fn add_to_analyzer_stream(query: &Query, analyzer_stream: &mut proc_macro2::Toke
393419
}
394420

395421
pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
396-
let queries = parse_macro_input!(input as List<Query>);
422+
let queries = parse_macro_input!(input as List<QueryEntry>);
397423

398424
let mut query_stream = quote! {};
425+
let mut non_query_stream = quote! {};
399426
let mut helpers = HelperTokenStreams::default();
400427
let mut analyzer_stream = quote! {};
401428
let mut errors = quote! {};
@@ -411,6 +438,18 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
411438
}
412439

413440
for query in queries.0 {
441+
let query = match query {
442+
QueryEntry::Query(query) => query,
443+
QueryEntry::NonQuery(NonQuery { doc_comments, name }) => {
444+
// Get the exceptional non-query case out of the way first.
445+
non_query_stream.extend(quote! {
446+
#(#doc_comments)*
447+
#name,
448+
});
449+
continue;
450+
}
451+
};
452+
414453
let Query { doc_comments, name, key_ty, return_ty, modifiers, .. } = &query;
415454

416455
// Normalize an absent return type into `-> ()` to make macro-rules parsing easier.
@@ -486,24 +525,18 @@ pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {
486525
let HelperTokenStreams { description_fns_stream, cache_on_disk_if_fns_stream } = helpers;
487526

488527
TokenStream::from(quote! {
489-
/// Higher-order macro that invokes the specified macro with a prepared
490-
/// list of all query signatures (including modifiers).
491-
///
492-
/// This allows multiple simpler macros to each have access to the list
493-
/// of queries.
528+
/// Higher-order macro that invokes the specified macro with (a) a list of all query
529+
/// signatures (including modifiers), and (b) a list of non-query names. This allows
530+
/// multiple simpler macros to each have access to these lists.
494531
#[macro_export]
495532
macro_rules! rustc_with_all_queries {
496533
(
497-
// The macro to invoke once, on all queries (plus extras).
534+
// The macro to invoke once, on all queries and non-queries.
498535
$macro:ident!
499-
500-
// Within [], an optional list of extra "query" signatures to
501-
// pass to the given macro, in addition to the actual queries.
502-
$( [$($extra_fake_queries:tt)*] )?
503536
) => {
504537
$macro! {
505-
$( $($extra_fake_queries)* )?
506-
#query_stream
538+
queries { #query_stream }
539+
non_queries { #non_query_stream }
507540
}
508541
}
509542
}

0 commit comments

Comments
 (0)