Skip to content

Commit 024757f

Browse files
committed
Auto merge of #153252 - matthiaskrgr:rollup-uYxeyaV, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #152042 (Suggest async block instead of async closure when possible) - #152949 (Introduce --ci flag in tidy) - #152655 (Disable debug_assert_not_in_new_nodes for multiple threads) - #153209 (Clean up `QueryVTable::hash_result` into `hash_value_fn`) - #153229 (rustfmt: add test for field representing type builtin syntax)
2 parents 765fd2d + d9c27fd commit 024757f

24 files changed

Lines changed: 377 additions & 146 deletions

File tree

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
99
use rustc_data_structures::profiling::QueryInvocationId;
1010
use rustc_data_structures::sharded::{self, ShardedHashMap};
1111
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
12-
use rustc_data_structures::sync::{AtomicU64, Lock};
12+
use rustc_data_structures::sync::{AtomicU64, Lock, is_dyn_thread_safe};
1313
use rustc_data_structures::unord::UnordMap;
1414
use rustc_data_structures::{assert_matches, outline};
1515
use rustc_errors::DiagInner;
@@ -1302,7 +1302,9 @@ impl CurrentDepGraph {
13021302
prev_graph: &SerializedDepGraph,
13031303
prev_index: SerializedDepNodeIndex,
13041304
) {
1305-
if let Some(ref nodes_in_current_session) = self.nodes_in_current_session {
1305+
if !is_dyn_thread_safe()
1306+
&& let Some(ref nodes_in_current_session) = self.nodes_in_current_session
1307+
{
13061308
debug_assert!(
13071309
!nodes_in_current_session
13081310
.lock()

compiler/rustc_middle/src/query/inner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ pub(crate) fn query_feed<'tcx, C>(
116116
// The query already has a cached value for this key.
117117
// That's OK if both values are the same, i.e. they have the same hash,
118118
// so now we check their hashes.
119-
if let Some(hasher_fn) = query_vtable.hash_result {
119+
if let Some(hash_value_fn) = query_vtable.hash_value_fn {
120120
let (old_hash, value_hash) = tcx.with_stable_hashing_context(|ref mut hcx| {
121-
(hasher_fn(hcx, &old), hasher_fn(hcx, &value))
121+
(hash_value_fn(hcx, &old), hash_value_fn(hcx, &value))
122122
});
123123
if old_hash != value_hash {
124124
// We have an inconsistency. This can happen if one of the two
@@ -151,7 +151,7 @@ pub(crate) fn query_feed<'tcx, C>(
151151
dep_node,
152152
tcx,
153153
&value,
154-
query_vtable.hash_result,
154+
query_vtable.hash_value_fn,
155155
query_vtable.format_value,
156156
);
157157
query_vtable.cache.complete(key, value, dep_node_index);

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
7474
pub type IsLoadableFromDiskFn<'tcx, Key> =
7575
fn(tcx: TyCtxt<'tcx>, key: &Key, index: SerializedDepNodeIndex) -> bool;
7676

77-
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
78-
7977
#[derive(Clone, Debug)]
8078
pub struct CycleError<I = QueryStackFrameExtra> {
8179
/// The query and related span that uses the cycle.
@@ -146,7 +144,12 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
146144

147145
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
148146
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
149-
pub hash_result: HashResult<C::Value>,
147+
148+
/// Function pointer that hashes this query's result values.
149+
///
150+
/// For `no_hash` queries, this function pointer is None.
151+
pub hash_value_fn: Option<fn(&mut StableHashingContext<'_>, &C::Value) -> Fingerprint>,
152+
150153
pub value_from_cycle_error:
151154
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
152155
pub format_value: fn(&C::Value) -> String,

compiler/rustc_query_impl/src/execution.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
372372
debug_assert_eq!(tcx.dep_graph.is_fully_enabled(), INCR);
373373

374374
// Delegate to another function to actually execute the query job.
375-
let (result, dep_node_index) = if INCR {
375+
let (value, dep_node_index) = if INCR {
376376
execute_job_incr(query, tcx, key, dep_node, id)
377377
} else {
378378
execute_job_non_incr(query, tcx, key, id)
@@ -384,18 +384,18 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
384384
// This can't happen, as query feeding adds the very dependencies to the fed query
385385
// as its feeding query had. So if the fed query is red, so is its feeder, which will
386386
// get evaluated first, and re-feed the query.
387-
if let Some((cached_result, _)) = cache.lookup(&key) {
388-
let Some(hasher) = query.hash_result else {
387+
if let Some((cached_value, _)) = cache.lookup(&key) {
388+
let Some(hash_value_fn) = query.hash_value_fn else {
389389
panic!(
390390
"no_hash fed query later has its value computed.\n\
391391
Remove `no_hash` modifier to allow recomputation.\n\
392392
The already cached value: {}",
393-
(query.format_value)(&cached_result)
393+
(query.format_value)(&cached_value)
394394
);
395395
};
396396

397397
let (old_hash, new_hash) = tcx.with_stable_hashing_context(|mut hcx| {
398-
(hasher(&mut hcx, &cached_result), hasher(&mut hcx, &result))
398+
(hash_value_fn(&mut hcx, &cached_value), hash_value_fn(&mut hcx, &value))
399399
});
400400
let formatter = query.format_value;
401401
if old_hash != new_hash {
@@ -407,17 +407,17 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
407407
computed={:#?}\nfed={:#?}",
408408
query.dep_kind,
409409
key,
410-
formatter(&result),
411-
formatter(&cached_result),
410+
formatter(&value),
411+
formatter(&cached_value),
412412
);
413413
}
414414
}
415415
}
416416

417417
// Tell the guard to perform completion bookkeeping, and also to not poison the query.
418-
job_guard.complete(cache, result, dep_node_index);
418+
job_guard.complete(cache, value, dep_node_index);
419419

420-
(result, Some(dep_node_index))
420+
(value, Some(dep_node_index))
421421
}
422422

423423
// Fast path for when incr. comp. is off.
@@ -438,22 +438,22 @@ fn execute_job_non_incr<'tcx, C: QueryCache>(
438438

439439
let prof_timer = tcx.prof.query_provider();
440440
// Call the query provider.
441-
let result =
441+
let value =
442442
start_query(tcx, job_id, query.depth_limit, || (query.invoke_provider_fn)(tcx, key));
443443
let dep_node_index = tcx.dep_graph.next_virtual_depnode_index();
444444
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
445445

446446
// Similarly, fingerprint the result to assert that
447447
// it doesn't have anything not considered hashable.
448448
if cfg!(debug_assertions)
449-
&& let Some(hash_result) = query.hash_result
449+
&& let Some(hash_value_fn) = query.hash_value_fn
450450
{
451451
tcx.with_stable_hashing_context(|mut hcx| {
452-
hash_result(&mut hcx, &result);
452+
hash_value_fn(&mut hcx, &value);
453453
});
454454
}
455455

456-
(result, dep_node_index)
456+
(value, dep_node_index)
457457
}
458458

459459
#[inline(always)]
@@ -509,7 +509,7 @@ fn execute_job_incr<'tcx, C: QueryCache>(
509509
tcx,
510510
(query, key),
511511
|tcx, (query, key)| (query.invoke_provider_fn)(tcx, key),
512-
query.hash_result,
512+
query.hash_value_fn,
513513
)
514514
});
515515

@@ -560,7 +560,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
560560
dep_graph_data,
561561
&value,
562562
prev_index,
563-
query.hash_result,
563+
query.hash_value_fn,
564564
query.format_value,
565565
);
566566
}
@@ -607,7 +607,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
607607
dep_graph_data,
608608
&value,
609609
prev_index,
610-
query.hash_result,
610+
query.hash_value_fn,
611611
query.format_value,
612612
);
613613

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,13 @@ macro_rules! is_feedable {
165165
};
166166
}
167167

168-
macro_rules! hash_result {
169-
([][$V:ty]) => {{
170-
Some(|hcx, result| {
171-
let result = rustc_middle::query::erase::restore_val::<$V>(*result);
172-
rustc_middle::dep_graph::hash_result(hcx, &result)
173-
})
174-
}};
175-
([(no_hash) $($rest:tt)*][$V:ty]) => {{
176-
None
177-
}};
178-
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
179-
hash_result!([$($modifiers)*][$($args)*])
180-
};
168+
/// Expands to `$yes` if the `no_hash` modifier is present, or `$no` otherwise.
169+
macro_rules! if_no_hash {
170+
([] $yes:tt $no:tt) => { $no };
171+
([(no_hash) $($modifiers:tt)*] $yes:tt $no:tt) => { $yes };
172+
([$other:tt $($modifiers:tt)*] $yes:tt $no:tt) => {
173+
if_no_hash!([$($modifiers)*] $yes $no)
174+
}
181175
}
182176

183177
macro_rules! call_provider {
@@ -560,7 +554,16 @@ macro_rules! define_queries {
560554
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
561555
erase::erase_val(result)
562556
},
563-
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),
557+
hash_value_fn: if_no_hash!(
558+
[$($modifiers)*]
559+
None
560+
{
561+
Some(|hcx, erased_value: &erase::Erased<queries::$name::Value<'tcx>>| {
562+
let value = erase::restore_val(*erased_value);
563+
rustc_middle::dep_graph::hash_result(hcx, &value)
564+
})
565+
}
566+
),
564567
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
565568
description_fn: $crate::queries::_description_fns::$name,
566569
execute_query_fn: if incremental {

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,44 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
798798
return false;
799799
}
800800

801+
// If this is a zero-argument async closure directly passed as an argument
802+
// and the expected type is `Future`, suggest using `async {}` block instead
803+
// of `async || {}`
804+
if let ty::CoroutineClosure(def_id, args) = *self_ty.kind()
805+
&& let sig = args.as_coroutine_closure().coroutine_closure_sig().skip_binder()
806+
&& let ty::Tuple(inputs) = *sig.tupled_inputs_ty.kind()
807+
&& inputs.is_empty()
808+
&& self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Future)
809+
&& let Some(hir::Node::Expr(hir::Expr {
810+
kind:
811+
hir::ExprKind::Closure(hir::Closure {
812+
kind: hir::ClosureKind::CoroutineClosure(CoroutineDesugaring::Async),
813+
fn_arg_span: Some(arg_span),
814+
..
815+
}),
816+
..
817+
})) = self.tcx.hir_get_if_local(def_id)
818+
&& obligation.cause.span.contains(*arg_span)
819+
{
820+
let sm = self.tcx.sess.source_map();
821+
let removal_span = if let Ok(snippet) =
822+
sm.span_to_snippet(arg_span.with_hi(arg_span.hi() + rustc_span::BytePos(1)))
823+
&& snippet.ends_with(' ')
824+
{
825+
// There's a space after `||`, include it in the removal
826+
arg_span.with_hi(arg_span.hi() + rustc_span::BytePos(1))
827+
} else {
828+
*arg_span
829+
};
830+
err.span_suggestion_verbose(
831+
removal_span,
832+
"use `async {}` instead of `async || {}` to introduce an async block",
833+
"",
834+
Applicability::MachineApplicable,
835+
);
836+
return true;
837+
}
838+
801839
// Get the name of the callable and the arguments to be used in the suggestion.
802840
let msg = match def_id_or_name {
803841
DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,9 @@ impl Step for Tidy {
13341334
if builder.config.cmd.bless() {
13351335
cmd.arg("--bless");
13361336
}
1337+
if builder.config.is_running_on_ci() {
1338+
cmd.arg("--ci=true");
1339+
}
13371340
if let Some(s) =
13381341
builder.config.cmd.extra_checks().or(builder.config.tidy_extra_checks.as_deref())
13391342
{
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type FRT = builtin # field_of(Type, ident);
2+
type FRT2 = builtin # field_of(Type<A, B>, ident);

src/tools/tidy/src/alphabetical/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::diagnostics::{TidyCtx, TidyFlags};
55

66
#[track_caller]
77
fn test(lines: &str, name: &str, expected_msg: &str, expected_bad: bool) {
8-
let tidy_ctx = TidyCtx::new(Path::new("/"), false, TidyFlags::default());
8+
let tidy_ctx = TidyCtx::new(Path::new("/"), false, None, TidyFlags::default());
99
let mut check = tidy_ctx.start_check("alphabetical-test");
1010
check_lines(Path::new(name), lines, &tidy_ctx, &mut check);
1111

@@ -37,7 +37,7 @@ fn bless_test(before: &str, after: &str) {
3737
let temp_path = tempfile::Builder::new().tempfile().unwrap().into_temp_path();
3838
std::fs::write(&temp_path, before).unwrap();
3939

40-
let tidy_ctx = TidyCtx::new(Path::new("/"), false, TidyFlags::new(true));
40+
let tidy_ctx = TidyCtx::new(Path::new("/"), false, None, TidyFlags::new(true));
4141

4242
let mut check = tidy_ctx.start_check("alphabetical-test");
4343
check_lines(&temp_path, before, &tidy_ctx, &mut check);

src/tools/tidy/src/arg_parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct TidyArgParser {
1515
pub npm: PathBuf,
1616
pub verbose: bool,
1717
pub bless: bool,
18+
pub ci: Option<bool>,
1819
pub extra_checks: Option<Vec<String>>,
1920
pub pos_args: Vec<String>,
2021
}
@@ -59,6 +60,7 @@ impl TidyArgParser {
5960
)
6061
.arg(Arg::new("verbose").help("verbose").long("verbose").action(ArgAction::SetTrue))
6162
.arg(Arg::new("bless").help("target files are modified").long("bless").action(ArgAction::SetTrue))
63+
.arg(Arg::new("ci").help("ci flag").long("ci").default_missing_value("true").num_args(0..=1).value_parser(value_parser!(bool)))
6264
.arg(
6365
Arg::new("extra_checks")
6466
.help("extra checks")
@@ -78,6 +80,7 @@ impl TidyArgParser {
7880
npm: matches.get_one::<PathBuf>("npm").unwrap().clone(),
7981
verbose: *matches.get_one::<bool>("verbose").unwrap(),
8082
bless: *matches.get_one::<bool>("bless").unwrap(),
83+
ci: matches.get_one::<bool>("ci").cloned(),
8184
extra_checks: None,
8285
pos_args: vec![],
8386
};

0 commit comments

Comments
 (0)