Skip to content

Commit 649feb9

Browse files
committed
Auto merge of rust-lang#127549 - jhpratt:rollup-o1mbmhr, r=jhpratt
Rollup of 8 pull requests Successful merges: - rust-lang#124211 (Bump `elided_lifetimes_in_associated_constant` to deny) - rust-lang#125627 (migration lint for `expr2024` for the edition 2024) - rust-lang#127091 (impl FusedIterator and a size hint for the error sources iter) - rust-lang#127461 (Fixup failing fuchsia tests) - rust-lang#127484 (`#[doc(alias)]`'s doc: say that ASCII spaces are allowed) - rust-lang#127508 (small search graph refactor) - rust-lang#127521 (Remove spastorino from SMIR) - rust-lang#127532 (documentation: update cmake version) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7caf672 + 08cb462 commit 649feb9

22 files changed

+324
-90
lines changed

INSTALL.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ If building LLVM from source, you'll need additional tools:
4848
[LLVM's documentation](https://llvm.org/docs/GettingStarted.html#host-c-toolchain-both-compiler-and-standard-library)
4949
* `ninja`, or GNU `make` 3.81 or later (Ninja is recommended, especially on
5050
Windows)
51-
* `cmake` 3.13.4 or later
51+
* `cmake` version listed on [LLVM's documentation](https://llvm.org/docs/GettingStarted.html#software)
5252
* `libstdc++-static` may be required on some Linux distributions such as Fedora
5353
and Ubuntu
5454

compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ lint_lintpass_by_hand = implementing `LintPass` by hand
439439
lint_macro_expanded_macro_exports_accessed_by_absolute_paths = macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
440440
.note = the macro is defined here
441441
442+
lint_macro_expr_fragment_specifier_2024_migration =
443+
the `expr` fragment specifier will accept more expressions in the 2024 edition
444+
.suggestion = to keep the existing behavior, use the `expr_2021` fragment specifier
442445
lint_macro_is_private = macro `{$ident}` is private
443446
444447
lint_macro_rule_never_used = rule #{$n} of macro `{$name}` is never used

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ mod late;
6060
mod let_underscore;
6161
mod levels;
6262
mod lints;
63+
mod macro_expr_fragment_specifier_2024_migration;
6364
mod map_unit_fn;
6465
mod methods;
6566
mod multiple_supertrait_upcastable;
@@ -97,6 +98,7 @@ use impl_trait_overcaptures::ImplTraitOvercaptures;
9798
use internal::*;
9899
use invalid_from_utf8::*;
99100
use let_underscore::*;
101+
use macro_expr_fragment_specifier_2024_migration::*;
100102
use map_unit_fn::*;
101103
use methods::*;
102104
use multiple_supertrait_upcastable::*;
@@ -170,6 +172,7 @@ early_lint_methods!(
170172
IncompleteInternalFeatures: IncompleteInternalFeatures,
171173
RedundantSemicolons: RedundantSemicolons,
172174
UnusedDocComment: UnusedDocComment,
175+
Expr2024: Expr2024,
173176
]
174177
]
175178
);

compiler/rustc_lint/src/lints.rs

+7
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,13 @@ pub struct BuiltinTypeAliasGenericBounds<'a, 'b> {
317317
pub sub: Option<SuggestChangingAssocTypes<'a, 'b>>,
318318
}
319319

320+
#[derive(LintDiagnostic)]
321+
#[diag(lint_macro_expr_fragment_specifier_2024_migration)]
322+
pub struct MacroExprFragment2024 {
323+
#[suggestion(code = "expr_2021", applicability = "machine-applicable")]
324+
pub suggestion: Span,
325+
}
326+
320327
pub struct BuiltinTypeAliasGenericBoundsSuggestion {
321328
pub suggestions: Vec<(Span, String)>,
322329
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
//! Migration code for the `expr_fragment_specifier_2024`
2+
//! rule.
3+
use tracing::debug;
4+
5+
use rustc_ast::token::Token;
6+
use rustc_ast::token::TokenKind;
7+
use rustc_ast::tokenstream::TokenStream;
8+
use rustc_ast::tokenstream::TokenTree;
9+
use rustc_session::declare_lint;
10+
use rustc_session::declare_lint_pass;
11+
use rustc_session::lint::FutureIncompatibilityReason;
12+
use rustc_span::edition::Edition;
13+
use rustc_span::sym;
14+
15+
use crate::lints::MacroExprFragment2024;
16+
use crate::EarlyLintPass;
17+
18+
declare_lint! {
19+
/// The `edition_2024_expr_fragment_specifier` lint detects the use of
20+
/// `expr` fragments in macros during migration to the 2024 edition.
21+
///
22+
/// The `expr` fragment specifier will accept more expressions in the 2024
23+
/// edition. To maintain the behavior from the 2021 edition and earlier, use
24+
/// the `expr_2021` fragment specifier.
25+
///
26+
/// ### Example
27+
///
28+
/// ```rust,edition2021,compile_fail
29+
/// #![deny(edition_2024_expr_fragment_specifier)]
30+
/// macro_rules! m {
31+
/// ($e:expr) => {
32+
/// $e
33+
/// }
34+
/// }
35+
///
36+
/// fn main() {
37+
/// m!(1);
38+
/// }
39+
/// ```
40+
///
41+
/// {{produces}}
42+
///
43+
/// ### Explanation
44+
///
45+
/// Rust [editions] allow the language to evolve without breaking backwards
46+
/// compatibility. This lint catches code that uses [macro matcher fragment
47+
/// specifiers] that have changed meaning in the 2024 edition. If you switch
48+
/// to the new edition without updating the code, your macros may behave
49+
/// differently.
50+
///
51+
/// In the 2024 edition, the `expr` fragment specifier `expr` will also
52+
/// match `const { ... }` blocks. This means if a macro had a pattern that
53+
/// matched `$e:expr` and another that matches `const { $e: expr }`, for
54+
/// example, that under the 2024 edition the first pattern would match while
55+
/// in the 2021 and earlier editions the second pattern would match. To keep
56+
/// the old behavior, use the `expr_2021` fragment specifier.
57+
///
58+
/// This lint detects macros whose behavior might change due to the changing
59+
/// meaning of the `expr` fragment specifier. It is "allow" by default
60+
/// because the code is perfectly valid in older editions. The [`cargo fix`]
61+
/// tool with the `--edition` flag will switch this lint to "warn" and
62+
/// automatically apply the suggested fix from the compiler. This provides a
63+
/// completely automated way to update old code for a new edition.
64+
///
65+
/// Using `cargo fix --edition` with this lint will ensure that your code
66+
/// retains the same behavior. This may not be the desired, as macro authors
67+
/// often will want their macros to use the latest grammar for matching
68+
/// expressions. Be sure to carefully review changes introduced by this lint
69+
/// to ensure the macros implement the desired behavior.
70+
///
71+
/// [editions]: https://doc.rust-lang.org/edition-guide/
72+
/// [macro matcher fragment specifiers]: https://doc.rust-lang.org/nightly/edition-guide/rust-2024/macro-fragment-specifiers.html
73+
/// [`cargo fix`]: https://doc.rust-lang.org/cargo/commands/cargo-fix.html
74+
pub EDITION_2024_EXPR_FRAGMENT_SPECIFIER,
75+
Allow,
76+
"The `expr` fragment specifier will accept more expressions in the 2024 edition. \
77+
To keep the existing behavior, use the `expr_2021` fragment specifier.",
78+
@future_incompatible = FutureIncompatibleInfo {
79+
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
80+
reference: "Migration Guide <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/macro-fragment-specifiers.html>",
81+
};
82+
}
83+
84+
declare_lint_pass!(Expr2024 => [EDITION_2024_EXPR_FRAGMENT_SPECIFIER,]);
85+
86+
impl Expr2024 {
87+
fn check_tokens(&mut self, cx: &crate::EarlyContext<'_>, tokens: &TokenStream) {
88+
let mut prev_colon = false;
89+
let mut prev_identifier = false;
90+
let mut prev_dollar = false;
91+
for tt in tokens.trees() {
92+
debug!(
93+
"check_tokens: {:?} - colon {prev_dollar} - ident {prev_identifier} - colon {prev_colon}",
94+
tt
95+
);
96+
match tt {
97+
TokenTree::Token(token, _) => match token.kind {
98+
TokenKind::Dollar => {
99+
prev_dollar = true;
100+
continue;
101+
}
102+
TokenKind::Ident(..) | TokenKind::NtIdent(..) => {
103+
if prev_colon && prev_identifier && prev_dollar {
104+
self.check_ident_token(cx, token);
105+
} else if prev_dollar {
106+
prev_identifier = true;
107+
continue;
108+
}
109+
}
110+
TokenKind::Colon => {
111+
if prev_dollar && prev_identifier {
112+
prev_colon = true;
113+
continue;
114+
}
115+
}
116+
_ => {}
117+
},
118+
TokenTree::Delimited(.., tts) => self.check_tokens(cx, tts),
119+
}
120+
prev_colon = false;
121+
prev_identifier = false;
122+
prev_dollar = false;
123+
}
124+
}
125+
126+
fn check_ident_token(&mut self, cx: &crate::EarlyContext<'_>, token: &Token) {
127+
debug!("check_ident_token: {:?}", token);
128+
let (sym, edition) = match token.kind {
129+
TokenKind::Ident(sym, _) => (sym, Edition::Edition2024),
130+
_ => return,
131+
};
132+
133+
debug!("token.span.edition(): {:?}", token.span.edition());
134+
if token.span.edition() >= edition {
135+
return;
136+
}
137+
138+
if sym != sym::expr {
139+
return;
140+
}
141+
142+
debug!("emitting lint");
143+
cx.builder.emit_span_lint(
144+
&EDITION_2024_EXPR_FRAGMENT_SPECIFIER,
145+
token.span.into(),
146+
MacroExprFragment2024 { suggestion: token.span },
147+
);
148+
}
149+
}
150+
151+
impl EarlyLintPass for Expr2024 {
152+
fn check_mac_def(&mut self, cx: &crate::EarlyContext<'_>, mc: &rustc_ast::MacroDef) {
153+
self.check_tokens(cx, &mc.body.tokens);
154+
}
155+
}

compiler/rustc_lint_defs/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4620,7 +4620,7 @@ declare_lint! {
46204620
/// [against]: https://github.com/rust-lang/rust/issues/38831
46214621
/// [future-incompatible]: ../index.md#future-incompatible-lints
46224622
pub ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4623-
Warn,
4623+
Deny,
46244624
"elided lifetimes cannot be used in associated constants in impls",
46254625
@future_incompatible = FutureIncompatibleInfo {
46264626
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,

compiler/rustc_next_trait_solver/src/solve/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ enum GoalEvaluationKind {
4848
Nested,
4949
}
5050

51+
// FIXME(trait-system-refactor-initiative#117): we don't detect whether a response
52+
// ended up pulling down any universes.
5153
fn has_no_inference_or_external_constraints<I: Interner>(
5254
response: ty::Canonical<I, Response<I>>,
5355
) -> bool {
54-
response.value.external_constraints.region_constraints.is_empty()
55-
&& response.value.var_values.is_identity()
56-
&& response.value.external_constraints.opaque_types.is_empty()
56+
let ExternalConstraintsData {
57+
ref region_constraints,
58+
ref opaque_types,
59+
ref normalization_nested_goals,
60+
} = *response.value.external_constraints;
61+
response.value.var_values.is_identity()
62+
&& region_constraints.is_empty()
63+
&& opaque_types.is_empty()
64+
&& normalization_nested_goals.is_empty()
5765
}
5866

5967
impl<'a, D, I> EvalCtxt<'a, D>

compiler/rustc_next_trait_solver/src/solve/search_graph.rs

+21-31
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct StackEntry<I: Interner> {
7171
/// C :- D
7272
/// D :- C
7373
/// ```
74-
cycle_participants: HashSet<CanonicalInput<I>>,
74+
nested_goals: HashSet<CanonicalInput<I>>,
7575
/// Starts out as `None` and gets set when rerunning this
7676
/// goal in case we encounter a cycle.
7777
provisional_result: Option<QueryResult<I>>,
@@ -139,18 +139,11 @@ impl<I: Interner> SearchGraph<I> {
139139
self.mode
140140
}
141141

142-
/// Pops the highest goal from the stack, lazily updating the
143-
/// the next goal in the stack.
144-
///
145-
/// Directly popping from the stack instead of using this method
146-
/// would cause us to not track overflow and recursion depth correctly.
147-
fn pop_stack(&mut self) -> StackEntry<I> {
148-
let elem = self.stack.pop().unwrap();
149-
if let Some(last) = self.stack.raw.last_mut() {
150-
last.reached_depth = last.reached_depth.max(elem.reached_depth);
151-
last.encountered_overflow |= elem.encountered_overflow;
142+
fn update_parent_goal(&mut self, reached_depth: StackDepth, encountered_overflow: bool) {
143+
if let Some(parent) = self.stack.raw.last_mut() {
144+
parent.reached_depth = parent.reached_depth.max(reached_depth);
145+
parent.encountered_overflow |= encountered_overflow;
152146
}
153-
elem
154147
}
155148

156149
pub(super) fn is_empty(&self) -> bool {
@@ -222,8 +215,8 @@ impl<I: Interner> SearchGraph<I> {
222215
let current_cycle_root = &mut stack[current_root.as_usize()];
223216
for entry in cycle_participants {
224217
entry.non_root_cycle_participant = entry.non_root_cycle_participant.max(Some(head));
225-
current_cycle_root.cycle_participants.insert(entry.input);
226-
current_cycle_root.cycle_participants.extend(mem::take(&mut entry.cycle_participants));
218+
current_cycle_root.nested_goals.insert(entry.input);
219+
current_cycle_root.nested_goals.extend(mem::take(&mut entry.nested_goals));
227220
}
228221
}
229222

@@ -342,7 +335,7 @@ impl<I: Interner> SearchGraph<I> {
342335
non_root_cycle_participant: None,
343336
encountered_overflow: false,
344337
has_been_used: HasBeenUsed::empty(),
345-
cycle_participants: Default::default(),
338+
nested_goals: Default::default(),
346339
provisional_result: None,
347340
};
348341
assert_eq!(self.stack.push(entry), depth);
@@ -364,14 +357,16 @@ impl<I: Interner> SearchGraph<I> {
364357
}
365358

366359
debug!("canonical cycle overflow");
367-
let current_entry = self.pop_stack();
360+
let current_entry = self.stack.pop().unwrap();
368361
debug_assert!(current_entry.has_been_used.is_empty());
369362
let result = Self::response_no_constraints(cx, input, Certainty::overflow(false));
370363
(current_entry, result)
371364
});
372365

373366
let proof_tree = inspect.finalize_canonical_goal_evaluation(cx);
374367

368+
self.update_parent_goal(final_entry.reached_depth, final_entry.encountered_overflow);
369+
375370
// We're now done with this goal. In case this goal is involved in a larger cycle
376371
// do not remove it from the provisional cache and update its provisional result.
377372
// We only add the root of cycles to the global cache.
@@ -394,15 +389,15 @@ impl<I: Interner> SearchGraph<I> {
394389
//
395390
// We must not use the global cache entry of a root goal if a cycle
396391
// participant is on the stack. This is necessary to prevent unstable
397-
// results. See the comment of `StackEntry::cycle_participants` for
392+
// results. See the comment of `StackEntry::nested_goals` for
398393
// more details.
399394
self.global_cache(cx).insert(
400395
cx,
401396
input,
402397
proof_tree,
403398
reached_depth,
404399
final_entry.encountered_overflow,
405-
final_entry.cycle_participants,
400+
final_entry.nested_goals,
406401
dep_node,
407402
result,
408403
)
@@ -441,14 +436,9 @@ impl<I: Interner> SearchGraph<I> {
441436
}
442437
}
443438

444-
// Update the reached depth of the current goal to make sure
445-
// its state is the same regardless of whether we've used the
446-
// global cache or not.
439+
// Adjust the parent goal as if we actually computed this goal.
447440
let reached_depth = self.stack.next_index().plus(additional_depth);
448-
if let Some(last) = self.stack.raw.last_mut() {
449-
last.reached_depth = last.reached_depth.max(reached_depth);
450-
last.encountered_overflow |= encountered_overflow;
451-
}
441+
self.update_parent_goal(reached_depth, encountered_overflow);
452442

453443
Some(result)
454444
}
@@ -477,7 +467,7 @@ impl<I: Interner> SearchGraph<I> {
477467
F: FnMut(&mut Self, &mut ProofTreeBuilder<D>) -> QueryResult<I>,
478468
{
479469
let result = prove_goal(self, inspect);
480-
let stack_entry = self.pop_stack();
470+
let stack_entry = self.stack.pop().unwrap();
481471
debug_assert_eq!(stack_entry.input, input);
482472

483473
// If the current goal is not the root of a cycle, we are done.
@@ -554,27 +544,27 @@ impl<I: Interner> SearchGraph<I> {
554544
non_root_cycle_participant,
555545
encountered_overflow: _,
556546
has_been_used,
557-
ref cycle_participants,
547+
ref nested_goals,
558548
provisional_result,
559549
} = *entry;
560550
let cache_entry = provisional_cache.get(&entry.input).unwrap();
561551
assert_eq!(cache_entry.stack_depth, Some(depth));
562552
if let Some(head) = non_root_cycle_participant {
563553
assert!(head < depth);
564-
assert!(cycle_participants.is_empty());
554+
assert!(nested_goals.is_empty());
565555
assert_ne!(stack[head].has_been_used, HasBeenUsed::empty());
566556

567557
let mut current_root = head;
568558
while let Some(parent) = stack[current_root].non_root_cycle_participant {
569559
current_root = parent;
570560
}
571-
assert!(stack[current_root].cycle_participants.contains(&input));
561+
assert!(stack[current_root].nested_goals.contains(&input));
572562
}
573563

574-
if !cycle_participants.is_empty() {
564+
if !nested_goals.is_empty() {
575565
assert!(provisional_result.is_some() || !has_been_used.is_empty());
576566
for entry in stack.iter().take(depth.as_usize()) {
577-
assert_eq!(cycle_participants.get(&entry.input), None);
567+
assert_eq!(nested_goals.get(&entry.input), None);
578568
}
579569
}
580570
}

0 commit comments

Comments
 (0)