Skip to content

Commit 21cad1f

Browse files
committed
Auto merge of #129249 - estebank:useless-into, r=<try>
[Experimental] `<T as Into<T>>::into` lint
2 parents c0bb140 + 6b3818c commit 21cad1f

44 files changed

Lines changed: 520 additions & 102 deletions

File tree

Some content is hidden

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

compiler/rustc_attr_parsing/src/attributes/cfg_select.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,30 @@ pub struct CfgSelectBranches {
4545
impl CfgSelectBranches {
4646
/// Removes the top-most branch for which `predicate` returns `true`,
4747
/// or the wildcard if none of the reachable branches satisfied the predicate.
48-
pub fn pop_first_match<F>(&mut self, predicate: F) -> Option<(TokenStream, Span)>
48+
pub fn pop_first_match<F>(&mut self, predicate: F) -> Option<(CfgEntry, TokenStream, Span)>
4949
where
5050
F: Fn(&CfgEntry) -> bool,
5151
{
5252
for (index, (cfg, _, _)) in self.reachable.iter().enumerate() {
5353
if predicate(cfg) {
54-
let matched = self.reachable.remove(index);
55-
return Some((matched.1, matched.2));
54+
return Some(self.reachable.remove(index));
5655
}
5756
}
5857

59-
self.wildcard.take().map(|(_, tts, span)| (tts, span))
58+
self.wildcard.take().map(|(_, tts, span)| (CfgEntry::Bool(true, span), tts, span))
6059
}
6160

6261
/// Consume this value and iterate over all the `TokenStream`s that it stores.
63-
pub fn into_iter_tts(self) -> impl Iterator<Item = (TokenStream, Span)> {
64-
let it1 = self.reachable.into_iter().map(|(_, tts, span)| (tts, span));
65-
let it2 = self.wildcard.into_iter().map(|(_, tts, span)| (tts, span));
66-
let it3 = self.unreachable.into_iter().map(|(_, tts, span)| (tts, span));
62+
pub fn into_iter_tts(self) -> impl Iterator<Item = (CfgEntry, TokenStream, Span)> {
63+
let it1 = self.reachable.into_iter().map(|(entry, tts, span)| (entry, tts, span));
64+
let it2 = self
65+
.wildcard
66+
.into_iter()
67+
.map(|(_, tts, span)| (CfgEntry::Bool(false, span), tts, span));
68+
let it3 = self
69+
.unreachable
70+
.into_iter()
71+
.map(|(_, tts, span)| (CfgEntry::Bool(false, span), tts, span));
6772

6873
it1.chain(it2).chain(it3)
6974
}

compiler/rustc_borrowck/src/region_infer/values.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,12 @@ impl ToElementIndex<'_> for RegionVid {
427427

428428
impl<'tcx> ToElementIndex<'tcx> for ty::PlaceholderRegion<'tcx> {
429429
fn add_to_row<N: Idx>(self, values: &mut RegionValues<'tcx, N>, row: N) -> bool {
430-
let placeholder: ty::PlaceholderRegion<'tcx> = self.into();
431-
let index = values.placeholder_indices.lookup_index(placeholder);
430+
let index = values.placeholder_indices.lookup_index(self);
432431
values.placeholders.insert(row, index)
433432
}
434433

435434
fn contained_in_row<N: Idx>(self, values: &RegionValues<'tcx, N>, row: N) -> bool {
436-
let placeholder: ty::PlaceholderRegion<'tcx> = self.into();
437-
let index = values.placeholder_indices.lookup_index(placeholder);
435+
let index = values.placeholder_indices.lookup_index(self);
438436
values.placeholders.contains(row, index)
439437
}
440438
}

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,7 @@ mod llvm_enzyme {
567567
PatKind::Ident(_, ident, _) => ecx.expr_path(ecx.path_ident(span, ident)),
568568
_ => todo!(),
569569
})
570-
.collect::<ThinVec<_>>()
571-
.into(),
570+
.collect::<ThinVec<_>>(),
572571
);
573572

574573
let enzyme_path_idents = ecx.std_path(&[sym::intrinsics, sym::autodiff]);

compiler/rustc_builtin_macros/src/cfg_select.rs

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
use rustc_ast::tokenstream::TokenStream;
2-
use rustc_ast::{Expr, ast};
1+
use rustc_ast::attr::mk_attr_from_item;
2+
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
3+
use rustc_ast::tokenstream::{
4+
AttrTokenStream, AttrTokenTree, DelimSpacing, DelimSpan, LazyAttrTokenStream, Spacing,
5+
TokenStream,
6+
};
7+
use rustc_ast::{AttrItem, AttrItemKind, EarlyParsedAttribute, Expr, Path, Safety, ast};
38
use rustc_attr_parsing as attr;
49
use rustc_attr_parsing::{CfgSelectBranches, EvalConfigResult, parse_cfg_select};
510
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacResult, MacroExpanderResult};
6-
use rustc_span::{Ident, Span, sym};
11+
use rustc_expand::expand::DeclaredIdents;
12+
use rustc_hir::attrs::CfgEntry;
13+
use rustc_span::{DUMMY_SP, Ident, Span, sym};
714
use smallvec::SmallVec;
815

916
use crate::errors::CfgSelectNoMatches;
@@ -17,6 +24,7 @@ struct CfgSelectResult<'cx, 'sess> {
1724
selected_tts: TokenStream,
1825
selected_span: Span,
1926
other_branches: CfgSelectBranches,
27+
cfg_entry: CfgEntry,
2028
}
2129

2230
fn tts_to_mac_result<'cx, 'sess>(
@@ -37,19 +45,99 @@ macro_rules! forward_to_parser_any_macro {
3745
fn $method_name(self: Box<Self>) -> Option<$ret_ty> {
3846
let CfgSelectResult { ecx, site_span, selected_tts, selected_span, .. } = *self;
3947

40-
for (tts, span) in self.other_branches.into_iter_tts() {
48+
for (_, tts, span) in self.other_branches.into_iter_tts() {
4149
let _ = tts_to_mac_result(ecx, site_span, tts, span).$method_name();
4250
}
4351

4452
tts_to_mac_result(ecx, site_span, selected_tts, selected_span).$method_name()
4553
}
4654
};
55+
(make_items) => {
56+
// The same logic as above, but we also register the items that were not selected in the
57+
// resolver for error reporting, as well as annotate the selected item with `#[cfg_trace]`.
58+
fn make_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::Item>; 1]>> {
59+
let CfgSelectResult { ecx, site_span, selected_tts, selected_span, cfg_entry, .. } =
60+
*self;
61+
62+
for (cfg_entry, tts, span) in self.other_branches.into_iter_tts() {
63+
if let Some(items) = tts_to_mac_result(ecx, site_span, tts, span).make_items() {
64+
// Register item names that were not selected for error reporting. We do this
65+
// for `#[cfg]` too.
66+
for item in items {
67+
for name in item.declared_idents() {
68+
ecx.resolver.append_stripped_cfg_item(
69+
ecx.current_expansion.lint_node_id,
70+
name,
71+
cfg_entry.clone(),
72+
span,
73+
);
74+
}
75+
}
76+
}
77+
}
78+
79+
tts_to_mac_result(ecx, site_span, selected_tts, selected_span).make_items().map(
80+
|items| {
81+
items
82+
.into_iter()
83+
.map(|mut item| {
84+
let g = &ecx.sess.psess.attr_id_generator;
85+
let args = AttrItemKind::Parsed(EarlyParsedAttribute::CfgTrace(
86+
cfg_entry.clone(),
87+
));
88+
let trees = vec![
89+
AttrTokenTree::Token(
90+
Token { kind: TokenKind::Pound, span: DUMMY_SP },
91+
Spacing::JointHidden,
92+
),
93+
AttrTokenTree::Delimited(
94+
DelimSpan::dummy(),
95+
DelimSpacing::new(Spacing::JointHidden, Spacing::Alone),
96+
Delimiter::Bracket,
97+
AttrTokenStream::new(vec![AttrTokenTree::Token(
98+
Token {
99+
kind: TokenKind::Ident(
100+
sym::cfg_trace,
101+
token::IdentIsRaw::No,
102+
),
103+
span: DUMMY_SP,
104+
},
105+
Spacing::Alone,
106+
)]),
107+
),
108+
];
109+
let tokens =
110+
Some(LazyAttrTokenStream::new_direct(AttrTokenStream::new(trees)));
111+
let attr_item = AttrItem {
112+
unsafety: Safety::Default,
113+
path: Path::from_ident(Ident::new(
114+
sym::cfg_trace,
115+
cfg_entry.span(),
116+
)),
117+
args,
118+
tokens: None,
119+
};
120+
let attr = mk_attr_from_item(
121+
g,
122+
attr_item,
123+
tokens,
124+
ast::AttrStyle::Outer,
125+
cfg_entry.span(),
126+
);
127+
item.attrs.push(attr);
128+
item
129+
})
130+
.collect()
131+
},
132+
)
133+
}
134+
};
47135
}
48136

49137
impl<'cx, 'sess> MacResult for CfgSelectResult<'cx, 'sess> {
50138
forward_to_parser_any_macro!(make_expr, Box<Expr>);
51139
forward_to_parser_any_macro!(make_stmts, SmallVec<[ast::Stmt; 1]>);
52-
forward_to_parser_any_macro!(make_items, SmallVec<[Box<ast::Item>; 1]>);
140+
forward_to_parser_any_macro!(make_items);
53141

54142
forward_to_parser_any_macro!(make_impl_items, SmallVec<[Box<ast::AssocItem>; 1]>);
55143
forward_to_parser_any_macro!(make_trait_impl_items, SmallVec<[Box<ast::AssocItem>; 1]>);
@@ -73,15 +161,18 @@ pub(super) fn expand_cfg_select<'cx>(
73161
ecx.current_expansion.lint_node_id,
74162
) {
75163
Ok(mut branches) => {
76-
if let Some((selected_tts, selected_span)) = branches.pop_first_match(|cfg| {
77-
matches!(attr::eval_config_entry(&ecx.sess, cfg), EvalConfigResult::True)
78-
}) {
164+
if let Some((cfg_entry, selected_tts, selected_span)) =
165+
branches.pop_first_match(|cfg| {
166+
matches!(attr::eval_config_entry(&ecx.sess, cfg), EvalConfigResult::True)
167+
})
168+
{
79169
let mac = CfgSelectResult {
80170
ecx,
81171
selected_tts,
82172
selected_span,
83173
other_branches: branches,
84174
site_span: sp,
175+
cfg_entry,
85176
};
86177
return ExpandResult::Ready(Box::new(mac));
87178
} else {

compiler/rustc_builtin_macros/src/format_foreign/shell/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn test_escape() {
2222
fn test_parse() {
2323
macro_rules! assert_pns_eq_sub {
2424
($in_:expr, $kind:ident($arg:expr, $pos:expr)) => {
25-
assert_eq!(pns(concat!($in_, "!")), Some((S::$kind($arg.into(), $pos), "!")))
25+
assert_eq!(pns(concat!($in_, "!")), Some((S::$kind($arg, $pos), "!")))
2626
};
2727
}
2828

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
371371
}
372372

373373
for i in 0..lane_count {
374-
let ret_lane = ret.place_lane(fx, i.into());
374+
let ret_lane = ret.place_lane(fx, i);
375375
ret_lane.write_cvalue(fx, value);
376376
}
377377
}

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
773773
_ => return interp_ok(false),
774774
}
775775

776-
trace!("{:?}", self.dump_place(&dest.clone().into()));
776+
trace!("{:?}", self.dump_place(&dest));
777777
self.return_to_block(ret)?;
778778
interp_ok(true)
779779
}

0 commit comments

Comments
 (0)