Skip to content

Commit f1ce0e6

Browse files
committed
Auto merge of #92587 - matthiaskrgr:rollup-qnwa8qx, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #92092 (Drop guards in slice sorting derive src pointers from &mut T, which is invalidated by interior mutation in comparison) - #92388 (Fix a minor mistake in `String::try_reserve_exact` examples) - #92442 (Add negative `impl` for `Ord`, `PartialOrd` on `LocalDefId`) - #92483 (Stabilize `result_cloned` and `result_copied`) - #92574 (Add RISC-V detection macro and more architecture instructions) - #92575 (ast: Always keep a `NodeId` in `ast::Crate`) - #92583 (:arrow_up: rust-analyzer) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 181e915 + 439a125 commit f1ce0e6

File tree

20 files changed

+77
-54
lines changed

20 files changed

+77
-54
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,10 @@ pub struct Crate {
510510
pub attrs: Vec<Attribute>,
511511
pub items: Vec<P<Item>>,
512512
pub span: Span,
513-
// Placeholder ID if the crate node is a macro placeholder.
514-
pub is_placeholder: Option<NodeId>,
513+
/// Must be equal to `CRATE_NODE_ID` after the crate root is expanded, but may hold
514+
/// expansion placeholders or an unassigned value (`DUMMY_NODE_ID`) before that.
515+
pub id: NodeId,
516+
pub is_placeholder: bool,
515517
}
516518

517519
/// Possible values inside of compile-time attribute lists.

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,8 @@ pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
11091109
}
11101110

11111111
pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
1112-
let Crate { attrs, items, span, is_placeholder: _ } = krate;
1112+
let Crate { attrs, items, span, id, is_placeholder: _ } = krate;
1113+
vis.visit_id(id);
11131114
visit_attrs(attrs, vis);
11141115
items.flat_map_in_place(|item| vis.flat_map_item(item));
11151116
vis.visit_span(span);
@@ -1554,6 +1555,7 @@ impl DummyAstNode for Crate {
15541555
attrs: Default::default(),
15551556
items: Default::default(),
15561557
span: Default::default(),
1558+
id: DUMMY_NODE_ID,
15571559
is_placeholder: Default::default(),
15581560
}
15591561
}

compiler/rustc_expand/src/expand.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
377377
dir_path,
378378
});
379379
let krate = self.fully_expand_fragment(AstFragment::Crate(krate)).make_crate();
380+
assert_eq!(krate.id, ast::CRATE_NODE_ID);
380381
self.cx.trace_macros_diag();
381382
krate
382383
}
@@ -1169,7 +1170,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
11691170
attrs: Vec::new(),
11701171
items: Vec::new(),
11711172
span,
1172-
is_placeholder: None,
1173+
id: self.cx.resolver.next_node_id(),
1174+
is_placeholder: false,
11731175
};
11741176
}
11751177
};
@@ -1180,7 +1182,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
11801182
.make_crate();
11811183
}
11821184

1183-
noop_visit_crate(&mut krate, self);
1185+
assign_id!(self, &mut krate.id, || noop_visit_crate(&mut krate, self));
11841186
krate
11851187
})
11861188
}

compiler/rustc_expand/src/placeholders.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub fn placeholder(
5050
attrs: Default::default(),
5151
items: Default::default(),
5252
span,
53-
is_placeholder: Some(id),
53+
id,
54+
is_placeholder: true,
5455
}),
5556
AstFragmentKind::Expr => AstFragment::Expr(expr_placeholder()),
5657
AstFragmentKind::OptExpr => AstFragment::OptExpr(Some(expr_placeholder())),
@@ -362,8 +363,8 @@ impl MutVisitor for PlaceholderExpander {
362363
}
363364

364365
fn visit_crate(&mut self, krate: &mut ast::Crate) {
365-
if let Some(id) = krate.is_placeholder {
366-
*krate = self.remove(id).make_crate();
366+
if krate.is_placeholder {
367+
*krate = self.remove(krate.id).make_crate();
367368
} else {
368369
noop_visit_crate(krate, self)
369370
}

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::proc_macro_decls;
33
use crate::util;
44

55
use rustc_ast::mut_visit::MutVisitor;
6-
use rustc_ast::{self as ast, visit};
6+
use rustc_ast::{self as ast, visit, DUMMY_NODE_ID};
77
use rustc_borrowck as mir_borrowck;
88
use rustc_codegen_ssa::back::link::emit_metadata;
99
use rustc_codegen_ssa::traits::CodegenBackend;
@@ -323,7 +323,7 @@ pub fn configure_and_expand(
323323

324324
let crate_attrs = krate.attrs.clone();
325325
let extern_mod_loaded = |ident: Ident, attrs, items, span| {
326-
let krate = ast::Crate { attrs, items, span, is_placeholder: None };
326+
let krate = ast::Crate { attrs, items, span, id: DUMMY_NODE_ID, is_placeholder: false };
327327
pre_expansion_lint(sess, lint_store, &krate, &crate_attrs, ident.name.as_str());
328328
(krate.attrs, krate.items)
329329
};

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'a> Parser<'a> {
2727
/// Parses a source module as a crate. This is the main entry point for the parser.
2828
pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
2929
let (attrs, items, span) = self.parse_mod(&token::Eof)?;
30-
Ok(ast::Crate { attrs, items, span, is_placeholder: None })
30+
Ok(ast::Crate { attrs, items, span, id: DUMMY_NODE_ID, is_placeholder: false })
3131
}
3232

3333
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,8 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
15121512
}
15131513

15141514
fn visit_crate(&mut self, krate: &'b ast::Crate) {
1515-
if let Some(id) = krate.is_placeholder {
1516-
self.visit_invoc_in_module(id);
1515+
if krate.is_placeholder {
1516+
self.visit_invoc_in_module(krate.id);
15171517
} else {
15181518
visit::walk_crate(self, krate);
15191519
self.contains_macro_use(&krate.attrs);

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
344344
}
345345

346346
fn visit_crate(&mut self, krate: &'a Crate) {
347-
if let Some(id) = krate.is_placeholder {
348-
self.visit_macro_invoc(id)
347+
if krate.is_placeholder {
348+
self.visit_macro_invoc(krate.id)
349349
} else {
350350
visit::walk_crate(self, krate)
351351
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use smallvec::{smallvec, SmallVec};
6868
use std::cell::{Cell, RefCell};
6969
use std::collections::{BTreeMap, BTreeSet};
7070
use std::ops::ControlFlow;
71-
use std::{cmp, fmt, iter, ptr};
71+
use std::{cmp, fmt, iter, mem, ptr};
7272
use tracing::debug;
7373

7474
use diagnostics::{extend_span_to_previous_binding, find_span_of_binding_until_next_binding};
@@ -1394,7 +1394,7 @@ impl<'a> Resolver<'a> {
13941394
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
13951395
.collect(),
13961396
lint_buffer: LintBuffer::default(),
1397-
next_node_id: NodeId::from_u32(1),
1397+
next_node_id: CRATE_NODE_ID,
13981398
node_id_to_def_id,
13991399
def_id_to_node_id,
14001400
placeholder_field_indices: Default::default(),
@@ -1430,8 +1430,7 @@ impl<'a> Resolver<'a> {
14301430
pub fn next_node_id(&mut self) -> NodeId {
14311431
let next =
14321432
self.next_node_id.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
1433-
self.next_node_id = ast::NodeId::from_u32(next);
1434-
self.next_node_id
1433+
mem::replace(&mut self.next_node_id, ast::NodeId::from_u32(next))
14351434
}
14361435

14371436
pub fn lint_buffer(&mut self) -> &mut LintBuffer {

compiler/rustc_span/src/def_id.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,23 @@ impl fmt::Debug for DefId {
316316

317317
rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId);
318318

319-
/// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
319+
/// A `LocalDefId` is equivalent to a `DefId` with `krate == LOCAL_CRATE`. Since
320320
/// we encode this information in the type, we can ensure at compile time that
321-
/// no DefIds from upstream crates get thrown into the mix. There are quite a
322-
/// few cases where we know that only DefIds from the local crate are expected
323-
/// and a DefId from a different crate would signify a bug somewhere. This
324-
/// is when LocalDefId comes in handy.
321+
/// no `DefId`s from upstream crates get thrown into the mix. There are quite a
322+
/// few cases where we know that only `DefId`s from the local crate are expected;
323+
/// a `DefId` from a different crate would signify a bug somewhere. This
324+
/// is when `LocalDefId` comes in handy.
325325
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
326326
pub struct LocalDefId {
327327
pub local_def_index: DefIndex,
328328
}
329329

330+
// To ensure correctness of incremental compilation,
331+
// `LocalDefId` must not implement `Ord` or `PartialOrd`.
332+
// See https://github.com/rust-lang/rust/issues/90317.
333+
impl !Ord for LocalDefId {}
334+
impl !PartialOrd for LocalDefId {}
335+
330336
pub const CRATE_DEF_ID: LocalDefId = LocalDefId { local_def_index: CRATE_DEF_INDEX };
331337

332338
impl Idx for LocalDefId {

0 commit comments

Comments
 (0)