Skip to content

Commit 0ca92de

Browse files
committed
Auto merge of rust-lang#127454 - matthiaskrgr:rollup-k3vfen2, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#127179 (Print `TypeId` as hex for debugging) - rust-lang#127189 (LinkedList's Cursor: method to get a ref to the cursor's list) - rust-lang#127236 (doc: update config file path in platform-support/wasm32-wasip1-threads.md) - rust-lang#127297 (Improve std::Path's Hash quality by avoiding prefix collisions) - rust-lang#127308 (Attribute cleanups) - rust-lang#127354 (Describe Sized requirements for mem::offset_of) - rust-lang#127409 (Emit a wrap expr span_bug only if context is not tainted) - rust-lang#127447 (once_lock: make test not take as long in Miri) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 959a2eb + b564c51 commit 0ca92de

File tree

22 files changed

+190
-134
lines changed

22 files changed

+190
-134
lines changed

compiler/rustc_ast/src/ast_traits.rs

-33
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use crate::{AssocItem, Expr, ForeignItem, Item, NodeId};
1010
use crate::{AttrItem, AttrKind, Block, Pat, Path, Ty, Visibility};
1111
use crate::{AttrVec, Attribute, Stmt, StmtKind};
1212

13-
use rustc_span::Span;
14-
1513
use std::fmt;
1614
use std::marker::PhantomData;
1715

@@ -91,37 +89,6 @@ impl<T: AstDeref<Target: HasNodeId>> HasNodeId for T {
9189
}
9290
}
9391

94-
/// A trait for AST nodes having a span.
95-
pub trait HasSpan {
96-
fn span(&self) -> Span;
97-
}
98-
99-
macro_rules! impl_has_span {
100-
($($T:ty),+ $(,)?) => {
101-
$(
102-
impl HasSpan for $T {
103-
fn span(&self) -> Span {
104-
self.span
105-
}
106-
}
107-
)+
108-
};
109-
}
110-
111-
impl_has_span!(AssocItem, Block, Expr, ForeignItem, Item, Pat, Path, Stmt, Ty, Visibility);
112-
113-
impl<T: AstDeref<Target: HasSpan>> HasSpan for T {
114-
fn span(&self) -> Span {
115-
self.ast_deref().span()
116-
}
117-
}
118-
119-
impl HasSpan for AttrItem {
120-
fn span(&self) -> Span {
121-
self.span()
122-
}
123-
}
124-
12592
/// A trait for AST nodes having (or not having) collected tokens.
12693
pub trait HasTokens {
12794
fn tokens(&self) -> Option<&LazyAttrTokenStream>;

compiler/rustc_ast/src/attr/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ impl Attribute {
202202
}
203203
}
204204

205-
pub fn tokens(&self) -> TokenStream {
205+
// Named `get_tokens` to distinguish it from the `<Attribute as HasTokens>::tokens` method.
206+
pub fn get_tokens(&self) -> TokenStream {
206207
match &self.kind {
207208
AttrKind::Normal(normal) => TokenStream::new(
208209
normal

compiler/rustc_ast/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub mod tokenstream;
4444
pub mod visit;
4545

4646
pub use self::ast::*;
47-
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasSpan, HasTokens};
47+
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
4848

4949
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5050

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ fn visit_attr_tt<T: MutVisitor>(tt: &mut AttrTokenTree, vis: &mut T) {
704704
visit_attr_tts(tts, vis);
705705
visit_delim_span(dspan, vis);
706706
}
707-
AttrTokenTree::Attributes(AttributesData { attrs, tokens }) => {
707+
AttrTokenTree::AttrsTarget(AttrsTarget { attrs, tokens }) => {
708708
visit_attrs(attrs, vis);
709709
visit_lazy_tts_opt_mut(Some(tokens), vis);
710710
}

compiler/rustc_ast/src/tokenstream.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! ownership of the original.
1515
1616
use crate::ast::{AttrStyle, StmtKind};
17-
use crate::ast_traits::{HasAttrs, HasSpan, HasTokens};
17+
use crate::ast_traits::{HasAttrs, HasTokens};
1818
use crate::token::{self, Delimiter, Nonterminal, Token, TokenKind};
1919
use crate::AttrVec;
2020

@@ -170,8 +170,8 @@ pub enum AttrTokenTree {
170170
Delimited(DelimSpan, DelimSpacing, Delimiter, AttrTokenStream),
171171
/// Stores the attributes for an attribute target,
172172
/// along with the tokens for that attribute target.
173-
/// See `AttributesData` for more information
174-
Attributes(AttributesData),
173+
/// See `AttrsTarget` for more information
174+
AttrsTarget(AttrsTarget),
175175
}
176176

177177
impl AttrTokenStream {
@@ -180,7 +180,7 @@ impl AttrTokenStream {
180180
}
181181

182182
/// Converts this `AttrTokenStream` to a plain `Vec<TokenTree>`.
183-
/// During conversion, `AttrTokenTree::Attributes` get 'flattened'
183+
/// During conversion, `AttrTokenTree::AttrsTarget` get 'flattened'
184184
/// back to a `TokenStream` of the form `outer_attr attr_target`.
185185
/// If there are inner attributes, they are inserted into the proper
186186
/// place in the attribute target tokens.
@@ -199,13 +199,13 @@ impl AttrTokenStream {
199199
TokenStream::new(stream.to_token_trees()),
200200
))
201201
}
202-
AttrTokenTree::Attributes(data) => {
203-
let idx = data
202+
AttrTokenTree::AttrsTarget(target) => {
203+
let idx = target
204204
.attrs
205205
.partition_point(|attr| matches!(attr.style, crate::AttrStyle::Outer));
206-
let (outer_attrs, inner_attrs) = data.attrs.split_at(idx);
206+
let (outer_attrs, inner_attrs) = target.attrs.split_at(idx);
207207

208-
let mut target_tokens = data.tokens.to_attr_token_stream().to_token_trees();
208+
let mut target_tokens = target.tokens.to_attr_token_stream().to_token_trees();
209209
if !inner_attrs.is_empty() {
210210
let mut found = false;
211211
// Check the last two trees (to account for a trailing semi)
@@ -227,7 +227,7 @@ impl AttrTokenStream {
227227

228228
let mut stream = TokenStream::default();
229229
for inner_attr in inner_attrs {
230-
stream.push_stream(inner_attr.tokens());
230+
stream.push_stream(inner_attr.get_tokens());
231231
}
232232
stream.push_stream(delim_tokens.clone());
233233
*tree = TokenTree::Delimited(*span, *spacing, *delim, stream);
@@ -242,7 +242,7 @@ impl AttrTokenStream {
242242
);
243243
}
244244
for attr in outer_attrs {
245-
res.extend(attr.tokens().0.iter().cloned());
245+
res.extend(attr.get_tokens().0.iter().cloned());
246246
}
247247
res.extend(target_tokens);
248248
}
@@ -262,7 +262,7 @@ impl AttrTokenStream {
262262
/// have an `attrs` field containing the `#[cfg(FALSE)]` attr,
263263
/// and a `tokens` field storing the (unparsed) tokens `struct Foo {}`
264264
#[derive(Clone, Debug, Encodable, Decodable)]
265-
pub struct AttributesData {
265+
pub struct AttrsTarget {
266266
/// Attributes, both outer and inner.
267267
/// These are stored in the original order that they were parsed in.
268268
pub attrs: AttrVec,
@@ -436,17 +436,17 @@ impl TokenStream {
436436
TokenStream::new(vec![TokenTree::token_alone(kind, span)])
437437
}
438438

439-
pub fn from_ast(node: &(impl HasAttrs + HasSpan + HasTokens + fmt::Debug)) -> TokenStream {
439+
pub fn from_ast(node: &(impl HasAttrs + HasTokens + fmt::Debug)) -> TokenStream {
440440
let Some(tokens) = node.tokens() else {
441-
panic!("missing tokens for node at {:?}: {:?}", node.span(), node);
441+
panic!("missing tokens for node: {:?}", node);
442442
};
443443
let attrs = node.attrs();
444444
let attr_stream = if attrs.is_empty() {
445445
tokens.to_attr_token_stream()
446446
} else {
447-
let attr_data =
448-
AttributesData { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
449-
AttrTokenStream::new(vec![AttrTokenTree::Attributes(attr_data)])
447+
let target =
448+
AttrsTarget { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
449+
AttrTokenStream::new(vec![AttrTokenTree::AttrsTarget(target)])
450450
};
451451
TokenStream::new(attr_stream.to_token_trees())
452452
}
@@ -765,6 +765,7 @@ mod size_asserts {
765765
static_assert_size!(AttrTokenStream, 8);
766766
static_assert_size!(AttrTokenTree, 32);
767767
static_assert_size!(LazyAttrTokenStream, 8);
768+
static_assert_size!(Option<LazyAttrTokenStream>, 8); // must be small, used in many AST nodes
768769
static_assert_size!(TokenStream, 8);
769770
static_assert_size!(TokenTree, 32);
770771
// tidy-alphabetical-end

compiler/rustc_builtin_macros/src/cfg_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl CfgEval<'_> {
193193

194194
// Re-parse the tokens, setting the `capture_cfg` flag to save extra information
195195
// to the captured `AttrTokenStream` (specifically, we capture
196-
// `AttrTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
196+
// `AttrTokenTree::AttrsTarget` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
197197
let mut parser = Parser::new(&self.0.sess.psess, orig_tokens, None);
198198
parser.capture_cfg = true;
199199
match parse_annotatable_with(&mut parser) {

compiler/rustc_expand/src/config.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<'a> StripUnconfigured<'a> {
172172
fn configure_tokens(&self, stream: &AttrTokenStream) -> AttrTokenStream {
173173
fn can_skip(stream: &AttrTokenStream) -> bool {
174174
stream.0.iter().all(|tree| match tree {
175-
AttrTokenTree::Attributes(_) => false,
175+
AttrTokenTree::AttrsTarget(_) => false,
176176
AttrTokenTree::Token(..) => true,
177177
AttrTokenTree::Delimited(.., inner) => can_skip(inner),
178178
})
@@ -185,22 +185,22 @@ impl<'a> StripUnconfigured<'a> {
185185
let trees: Vec<_> = stream
186186
.0
187187
.iter()
188-
.flat_map(|tree| match tree.clone() {
189-
AttrTokenTree::Attributes(mut data) => {
190-
data.attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
188+
.filter_map(|tree| match tree.clone() {
189+
AttrTokenTree::AttrsTarget(mut target) => {
190+
target.attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
191191

192-
if self.in_cfg(&data.attrs) {
193-
data.tokens = LazyAttrTokenStream::new(
194-
self.configure_tokens(&data.tokens.to_attr_token_stream()),
192+
if self.in_cfg(&target.attrs) {
193+
target.tokens = LazyAttrTokenStream::new(
194+
self.configure_tokens(&target.tokens.to_attr_token_stream()),
195195
);
196-
Some(AttrTokenTree::Attributes(data)).into_iter()
196+
Some(AttrTokenTree::AttrsTarget(target))
197197
} else {
198-
None.into_iter()
198+
None
199199
}
200200
}
201201
AttrTokenTree::Delimited(sp, spacing, delim, mut inner) => {
202202
inner = self.configure_tokens(&inner);
203-
Some(AttrTokenTree::Delimited(sp, spacing, delim, inner)).into_iter()
203+
Some(AttrTokenTree::Delimited(sp, spacing, delim, inner))
204204
}
205205
AttrTokenTree::Token(
206206
Token {
@@ -220,9 +220,7 @@ impl<'a> StripUnconfigured<'a> {
220220
) => {
221221
panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tree);
222222
}
223-
AttrTokenTree::Token(token, spacing) => {
224-
Some(AttrTokenTree::Token(token, spacing)).into_iter()
225-
}
223+
AttrTokenTree::Token(token, spacing) => Some(AttrTokenTree::Token(token, spacing)),
226224
})
227225
.collect();
228226
AttrTokenStream::new(trees)
@@ -294,7 +292,7 @@ impl<'a> StripUnconfigured<'a> {
294292
attr: &Attribute,
295293
(item, item_span): (ast::AttrItem, Span),
296294
) -> Attribute {
297-
let orig_tokens = attr.tokens();
295+
let orig_tokens = attr.get_tokens();
298296

299297
// We are taking an attribute of the form `#[cfg_attr(pred, attr)]`
300298
// and producing an attribute of the form `#[attr]`. We
@@ -310,12 +308,11 @@ impl<'a> StripUnconfigured<'a> {
310308
else {
311309
panic!("Bad tokens for attribute {attr:?}");
312310
};
313-
let pound_span = pound_token.span;
314311

315312
// We don't really have a good span to use for the synthesized `[]`
316313
// in `#[attr]`, so just use the span of the `#` token.
317314
let bracket_group = AttrTokenTree::Delimited(
318-
DelimSpan::from_single(pound_span),
315+
DelimSpan::from_single(pound_token.span),
319316
DelimSpacing::new(Spacing::JointHidden, Spacing::Alone),
320317
Delimiter::Bracket,
321318
item.tokens

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
734734
// struct; however, when EUV is run during typeck, it
735735
// may not. This will generate an error earlier in typeck,
736736
// so we can just ignore it.
737-
span_bug!(with_expr.span, "with expression doesn't evaluate to a struct");
737+
if self.cx.tainted_by_errors().is_ok() {
738+
span_bug!(with_expr.span, "with expression doesn't evaluate to a struct");
739+
}
738740
}
739741
}
740742

compiler/rustc_parse/src/parser/attr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl<'a> Parser<'a> {
282282
pub fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
283283
let mut attrs = ast::AttrVec::new();
284284
loop {
285-
let start_pos: u32 = self.num_bump_calls.try_into().unwrap();
285+
let start_pos = self.num_bump_calls;
286286
// Only try to parse if it is an inner attribute (has `!`).
287287
let attr = if self.check(&token::Pound) && self.look_ahead(1, |t| t == &token::Not) {
288288
Some(self.parse_attribute(InnerAttrPolicy::Permitted)?)
@@ -303,7 +303,7 @@ impl<'a> Parser<'a> {
303303
None
304304
};
305305
if let Some(attr) = attr {
306-
let end_pos: u32 = self.num_bump_calls.try_into().unwrap();
306+
let end_pos = self.num_bump_calls;
307307
// If we are currently capturing tokens, mark the location of this inner attribute.
308308
// If capturing ends up creating a `LazyAttrTokenStream`, we will include
309309
// this replace range with it, removing the inner attribute from the final
@@ -313,7 +313,7 @@ impl<'a> Parser<'a> {
313313
// corresponding macro).
314314
let range = start_pos..end_pos;
315315
if let Capturing::Yes = self.capture_state.capturing {
316-
self.capture_state.inner_attr_ranges.insert(attr.id, (range, vec![]));
316+
self.capture_state.inner_attr_ranges.insert(attr.id, (range, None));
317317
}
318318
attrs.push(attr);
319319
} else {

0 commit comments

Comments
 (0)