Skip to content

Commit f9cb39b

Browse files
committed
Auto merge of #142020 - matthiaskrgr:rollup-8g1r496, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #141271 (Streamline some attr parsing APIs) - #141570 (Fix incorrect eq_unspanned in TokenStream) - #141857 (coretests: move float tests from num to floats module and use a more flexible macro to generate them) - #141893 (remove `f16: From<u16>`) - #141924 (Lightly tweak docs for BTree{Map,Set}::extract_if) - #141939 (exact_div: add tests) - #141959 (Add more missing 2015 edition directives) - #142002 (redesign stage 0 std follow-ups part2) - #142007 (Improve some `Visitor` comments.) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 61413ae + 182e9f6 commit f9cb39b

File tree

59 files changed

+758
-518
lines changed

Some content is hidden

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

59 files changed

+758
-518
lines changed

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ impl TokenTree {
5757
match (self, other) {
5858
(TokenTree::Token(token, _), TokenTree::Token(token2, _)) => token.kind == token2.kind,
5959
(TokenTree::Delimited(.., delim, tts), TokenTree::Delimited(.., delim2, tts2)) => {
60-
delim == delim2 && tts.eq_unspanned(tts2)
60+
delim == delim2
61+
&& tts.len() == tts2.len()
62+
&& tts.iter().zip(tts2.iter()).all(|(a, b)| a.eq_unspanned(b))
6163
}
6264
_ => false,
6365
}
@@ -694,18 +696,6 @@ impl TokenStream {
694696
TokenStreamIter::new(self)
695697
}
696698

697-
/// Compares two `TokenStream`s, checking equality without regarding span information.
698-
pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
699-
let mut iter1 = self.iter();
700-
let mut iter2 = other.iter();
701-
for (tt1, tt2) in iter::zip(&mut iter1, &mut iter2) {
702-
if !tt1.eq_unspanned(tt2) {
703-
return false;
704-
}
705-
}
706-
iter1.next().is_none() && iter2.next().is_none()
707-
}
708-
709699
/// Create a token stream containing a single token with alone spacing. The
710700
/// spacing used for the final token in a constructed stream doesn't matter
711701
/// because it's never used. In practice we arbitrarily use

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ pub enum LifetimeCtxt {
121121
/// explicitly, you need to override each method. (And you also need
122122
/// to monitor future changes to `Visitor` in case a new method with a
123123
/// new default implementation gets introduced.)
124+
///
125+
/// Every `walk_*` method uses deconstruction to access fields of structs and
126+
/// enums. This will result in a compile error if a field is added, which makes
127+
/// it more likely the appropriate visit call will be added for it.
124128
pub trait Visitor<'ast>: Sized {
125129
/// The result type of the `visit_*` methods. Can be either `()`,
126130
/// or `ControlFlow<T>`.

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn parse_unstable<'a>(
5353

5454
for param in list.mixed() {
5555
let param_span = param.span();
56-
if let Some(ident) = param.meta_item().and_then(|i| i.path_without_args().word()) {
56+
if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
5757
res.push(ident.name);
5858
} else {
5959
cx.emit_err(session_diagnostics::ExpectsFeatures {

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl SingleAttributeParser for DeprecationParser {
7979
return None;
8080
};
8181

82-
let ident_name = param.path_without_args().word_sym();
82+
let ident_name = param.path().word_sym();
8383

8484
match ident_name {
8585
Some(name @ sym::since) => {
@@ -102,7 +102,7 @@ impl SingleAttributeParser for DeprecationParser {
102102
_ => {
103103
cx.emit_err(session_diagnostics::UnknownMetaItem {
104104
span: param_span,
105-
item: param.path_without_args().to_string(),
105+
item: param.path().to_string(),
106106
expected: if features.deprecated_suggestion() {
107107
&["since", "note", "suggestion"]
108108
} else {

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
9696

9797
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
9898
// structure.
99-
let (name, ident_span) = if let Some(ident) = param.path_without_args().word() {
99+
let (name, ident_span) = if let Some(ident) = param.path().word() {
100100
(Some(ident.name), ident.span)
101101
} else {
102102
(None, DUMMY_SP)

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn insert_value_into_option_or_error(
204204
if item.is_some() {
205205
cx.emit_err(session_diagnostics::MultipleItem {
206206
span: param.span(),
207-
item: param.path_without_args().to_string(),
207+
item: param.path().to_string(),
208208
});
209209
None
210210
} else if let Some(v) = param.args().name_value()
@@ -242,13 +242,13 @@ pub(crate) fn parse_stability(
242242
return None;
243243
};
244244

245-
match param.path_without_args().word_sym() {
245+
match param.path().word_sym() {
246246
Some(sym::feature) => insert_value_into_option_or_error(cx, &param, &mut feature)?,
247247
Some(sym::since) => insert_value_into_option_or_error(cx, &param, &mut since)?,
248248
_ => {
249249
cx.emit_err(session_diagnostics::UnknownMetaItem {
250250
span: param_span,
251-
item: param.path_without_args().to_string(),
251+
item: param.path().to_string(),
252252
expected: &["feature", "since"],
253253
});
254254
return None;
@@ -310,7 +310,7 @@ pub(crate) fn parse_unstability(
310310
return None;
311311
};
312312

313-
match param.path_without_args().word_sym() {
313+
match param.path().word_sym() {
314314
Some(sym::feature) => insert_value_into_option_or_error(cx, &param, &mut feature)?,
315315
Some(sym::reason) => insert_value_into_option_or_error(cx, &param, &mut reason)?,
316316
Some(sym::issue) => {
@@ -349,7 +349,7 @@ pub(crate) fn parse_unstability(
349349
_ => {
350350
cx.emit_err(session_diagnostics::UnknownMetaItem {
351351
span: param.span(),
352-
item: param.path_without_args().to_string(),
352+
item: param.path().to_string(),
353353
expected: &["feature", "reason", "issue", "soft", "implied_by"],
354354
});
355355
return None;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ impl<'sess> AttributeParser<'sess> {
264264
// }
265265
ast::AttrKind::Normal(n) => {
266266
let parser = MetaItemParser::from_attr(n, self.dcx());
267-
let (path, args) = parser.deconstruct();
267+
let path = parser.path();
268+
let args = parser.args();
268269
let parts = path.segments().map(|i| i.name).collect::<Vec<_>>();
269270

270271
if let Some(accept) = ATTRIBUTE_MAPPING.0.get(parts.as_slice()) {

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -252,35 +252,18 @@ impl<'a> MetaItemParser<'a> {
252252
}
253253
}
254254

255-
/// Gets just the path, without the args.
256-
pub fn path_without_args(&self) -> PathParser<'a> {
257-
self.path.clone()
258-
}
259-
260-
/// Gets just the args parser, without caring about the path.
261-
pub fn args(&self) -> &ArgParser<'a> {
262-
&self.args
263-
}
264-
265-
pub fn deconstruct(&self) -> (PathParser<'a>, &ArgParser<'a>) {
266-
(self.path_without_args(), self.args())
267-
}
268-
269-
/// Asserts that this MetaItem starts with a path. Some examples:
255+
/// Gets just the path, without the args. Some examples:
270256
///
271257
/// - `#[rustfmt::skip]`: `rustfmt::skip` is a path
272258
/// - `#[allow(clippy::complexity)]`: `clippy::complexity` is a path
273259
/// - `#[inline]`: `inline` is a single segment path
274-
pub fn path(&self) -> (PathParser<'a>, &ArgParser<'a>) {
275-
self.deconstruct()
260+
pub fn path(&self) -> &PathParser<'a> {
261+
&self.path
276262
}
277263

278-
/// Asserts that this MetaItem starts with a word, or single segment path.
279-
/// Doesn't return the args parser.
280-
///
281-
/// For examples. see [`Self::word`]
282-
pub fn word_without_args(&self) -> Option<Ident> {
283-
Some(self.word()?.0)
264+
/// Gets just the args parser, without caring about the path.
265+
pub fn args(&self) -> &ArgParser<'a> {
266+
&self.args
284267
}
285268

286269
/// Asserts that this MetaItem starts with a word, or single segment path.
@@ -289,23 +272,8 @@ impl<'a> MetaItemParser<'a> {
289272
/// - `#[inline]`: `inline` is a word
290273
/// - `#[rustfmt::skip]`: `rustfmt::skip` is a path,
291274
/// and not a word and should instead be parsed using [`path`](Self::path)
292-
pub fn word(&self) -> Option<(Ident, &ArgParser<'a>)> {
293-
let (path, args) = self.deconstruct();
294-
Some((path.word()?, args))
295-
}
296-
297-
/// Asserts that this MetaItem starts with some specific word.
298-
///
299-
/// See [`word`](Self::word) for examples of what a word is.
300275
pub fn word_is(&self, sym: Symbol) -> Option<&ArgParser<'a>> {
301-
self.path_without_args().word_is(sym).then(|| self.args())
302-
}
303-
304-
/// Asserts that this MetaItem starts with some specific path.
305-
///
306-
/// See [`word`](Self::path) for examples of what a word is.
307-
pub fn path_is(&self, segments: &[Symbol]) -> Option<&ArgParser<'a>> {
308-
self.path_without_args().segments_is(segments).then(|| self.args())
276+
self.path().word_is(sym).then(|| self.args())
309277
}
310278
}
311279

@@ -548,7 +516,7 @@ impl<'a> MetaItemListParser<'a> {
548516
}
549517

550518
/// Lets you pick and choose as what you want to parse each element in the list
551-
pub fn mixed<'s>(&'s self) -> impl Iterator<Item = &'s MetaItemOrLitParser<'a>> + 's {
519+
pub fn mixed(&self) -> impl Iterator<Item = &MetaItemOrLitParser<'a>> {
552520
self.sub_parsers.iter()
553521
}
554522

@@ -560,20 +528,6 @@ impl<'a> MetaItemListParser<'a> {
560528
self.len() == 0
561529
}
562530

563-
/// Asserts that every item in the list is another list starting with a word.
564-
///
565-
/// See [`MetaItemParser::word`] for examples of words.
566-
pub fn all_word_list<'s>(&'s self) -> Option<Vec<(Ident, &'s ArgParser<'a>)>> {
567-
self.mixed().map(|i| i.meta_item()?.word()).collect()
568-
}
569-
570-
/// Asserts that every item in the list is another list starting with a full path.
571-
///
572-
/// See [`MetaItemParser::path`] for examples of paths.
573-
pub fn all_path_list<'s>(&'s self) -> Option<Vec<(PathParser<'a>, &'s ArgParser<'a>)>> {
574-
self.mixed().map(|i| Some(i.meta_item()?.path())).collect()
575-
}
576-
577531
/// Returns Some if the list contains only a single element.
578532
///
579533
/// Inside the Some is the parser to parse this single element.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ use nested_filter::NestedFilter;
200200
/// explicitly, you need to override each method. (And you also need
201201
/// to monitor future changes to `Visitor` in case a new method with a
202202
/// new default implementation gets introduced.)
203+
///
204+
/// Every `walk_*` method uses deconstruction to access fields of structs and
205+
/// enums. This will result in a compile error if a field is added, which makes
206+
/// it more likely the appropriate visit call will be added for it.
203207
pub trait Visitor<'v>: Sized {
204208
// This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`.
205209
type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt;
@@ -1201,7 +1205,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(
12011205
visitor: &mut V,
12021206
trait_item: &'v TraitItem<'v>,
12031207
) -> V::Result {
1204-
// N.B., deliberately force a compilation error if/when new fields are added.
12051208
let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
12061209
let hir_id = trait_item.hir_id();
12071210
try_visit!(visitor.visit_ident(ident));
@@ -1240,7 +1243,6 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(
12401243
visitor: &mut V,
12411244
trait_item_ref: &'v TraitItemRef,
12421245
) -> V::Result {
1243-
// N.B., deliberately force a compilation error if/when new fields are added.
12441246
let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
12451247
try_visit!(visitor.visit_nested_trait_item(id));
12461248
try_visit!(visitor.visit_ident(ident));
@@ -1251,7 +1253,6 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(
12511253
visitor: &mut V,
12521254
impl_item: &'v ImplItem<'v>,
12531255
) -> V::Result {
1254-
// N.B., deliberately force a compilation error if/when new fields are added.
12551256
let ImplItem {
12561257
owner_id: _,
12571258
ident,
@@ -1286,7 +1287,6 @@ pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
12861287
visitor: &mut V,
12871288
foreign_item_ref: &'v ForeignItemRef,
12881289
) -> V::Result {
1289-
// N.B., deliberately force a compilation error if/when new fields are added.
12901290
let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
12911291
try_visit!(visitor.visit_nested_foreign_item(id));
12921292
visitor.visit_ident(ident)
@@ -1296,7 +1296,6 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
12961296
visitor: &mut V,
12971297
impl_item_ref: &'v ImplItemRef,
12981298
) -> V::Result {
1299-
// N.B., deliberately force a compilation error if/when new fields are added.
13001299
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
13011300
try_visit!(visitor.visit_nested_impl_item(id));
13021301
try_visit!(visitor.visit_ident(ident));

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use super::{
33
Pat, PatKind, Stmt, StmtKind, Thir,
44
};
55

6+
/// Every `walk_*` method uses deconstruction to access fields of structs and
7+
/// enums. This will result in a compile error if a field is added, which makes
8+
/// it more likely the appropriate visit call will be added for it.
69
pub trait Visitor<'thir, 'tcx: 'thir>: Sized {
710
fn thir(&self) -> &'thir Thir<'tcx>;
811

compiler/rustc_parse/src/parser/tokenstream/tests.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ fn sp(a: u32, b: u32) -> Span {
1414
Span::with_root_ctxt(BytePos(a), BytePos(b))
1515
}
1616

17+
fn cmp_token_stream(a: &TokenStream, b: &TokenStream) -> bool {
18+
a.len() == b.len() && a.iter().zip(b.iter()).all(|(x, y)| x.eq_unspanned(y))
19+
}
20+
1721
#[test]
1822
fn test_concat() {
1923
create_default_session_globals_then(|| {
@@ -25,7 +29,7 @@ fn test_concat() {
2529
eq_res.push_stream(test_snd);
2630
assert_eq!(test_res.iter().count(), 5);
2731
assert_eq!(eq_res.iter().count(), 5);
28-
assert_eq!(test_res.eq_unspanned(&eq_res), true);
32+
assert_eq!(cmp_token_stream(&test_res, &eq_res), true);
2933
})
3034
}
3135

@@ -104,7 +108,7 @@ fn test_dotdotdot() {
104108
stream.push_tree(TokenTree::token_joint(token::Dot, sp(0, 1)));
105109
stream.push_tree(TokenTree::token_joint(token::Dot, sp(1, 2)));
106110
stream.push_tree(TokenTree::token_alone(token::Dot, sp(2, 3)));
107-
assert!(stream.eq_unspanned(&string_to_ts("...")));
111+
assert!(cmp_token_stream(&stream, &string_to_ts("...")));
108112
assert_eq!(stream.iter().count(), 1);
109113
})
110114
}

library/alloc/src/collections/btree/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,18 +1416,18 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14161416
///
14171417
/// # Examples
14181418
///
1419-
/// Splitting a map into even and odd keys, reusing the original map:
1420-
///
14211419
/// ```
14221420
/// #![feature(btree_extract_if)]
14231421
/// use std::collections::BTreeMap;
14241422
///
1423+
/// // Splitting a map into even and odd keys, reusing the original map:
14251424
/// let mut map: BTreeMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
14261425
/// let evens: BTreeMap<_, _> = map.extract_if(.., |k, _v| k % 2 == 0).collect();
14271426
/// let odds = map;
14281427
/// assert_eq!(evens.keys().copied().collect::<Vec<_>>(), [0, 2, 4, 6]);
14291428
/// assert_eq!(odds.keys().copied().collect::<Vec<_>>(), [1, 3, 5, 7]);
14301429
///
1430+
/// // Splitting a map into low and high halves, reusing the original map:
14311431
/// let mut map: BTreeMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
14321432
/// let low: BTreeMap<_, _> = map.extract_if(0..4, |_k, _v| true).collect();
14331433
/// let high = map;

library/alloc/src/collections/btree/set.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,21 +1201,21 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
12011201
/// [`retain`]: BTreeSet::retain
12021202
/// # Examples
12031203
///
1204-
/// Splitting a set into even and odd values, reusing the original set:
1205-
///
12061204
/// ```
12071205
/// #![feature(btree_extract_if)]
12081206
/// use std::collections::BTreeSet;
12091207
///
1208+
/// // Splitting a set into even and odd values, reusing the original set:
12101209
/// let mut set: BTreeSet<i32> = (0..8).collect();
12111210
/// let evens: BTreeSet<_> = set.extract_if(.., |v| v % 2 == 0).collect();
12121211
/// let odds = set;
12131212
/// assert_eq!(evens.into_iter().collect::<Vec<_>>(), vec![0, 2, 4, 6]);
12141213
/// assert_eq!(odds.into_iter().collect::<Vec<_>>(), vec![1, 3, 5, 7]);
12151214
///
1216-
/// let mut map: BTreeSet<i32> = (0..8).collect();
1217-
/// let low: BTreeSet<_> = map.extract_if(0..4, |_v| true).collect();
1218-
/// let high = map;
1215+
/// // Splitting a set into low and high halves, reusing the original set:
1216+
/// let mut set: BTreeSet<i32> = (0..8).collect();
1217+
/// let low: BTreeSet<_> = set.extract_if(0..4, |_v| true).collect();
1218+
/// let high = set;
12191219
/// assert_eq!(low.into_iter().collect::<Vec<_>>(), [0, 1, 2, 3]);
12201220
/// assert_eq!(high.into_iter().collect::<Vec<_>>(), [4, 5, 6, 7]);
12211221
/// ```

library/core/src/convert/num.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ impl_from!(u8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")
175175
impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
176176
impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
177177
impl_from!(u8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
178-
impl_from!(u16 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
179178
impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
180179
impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
181180
impl_from!(u16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);

0 commit comments

Comments
 (0)