Skip to content

Commit 27fb598

Browse files
committed
Auto merge of #114905 - matthiaskrgr:rollup-uxhhr7p, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #113115 (we are migrating to askama) - #114784 (Improve `invalid_reference_casting` lint) - #114822 (Improve code readability by moving fmt args directly into the string) - #114878 (rustc book: make more pleasant to search) - #114899 (Add missing Clone/Debug impls to SMIR Trait related tys) r? `@ghost` `@rustbot` modify labels: rollup
2 parents ce01f4d + f6c20ad commit 27fb598

Some content is hidden

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

44 files changed

+486
-372
lines changed

compiler/rustc_lint/src/reference_casting.rs

+31-18
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>)
100100

101101
fn from_casts<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
102102
// <expr> as *mut ...
103-
let e = if let ExprKind::Cast(e, t) = e.kind
103+
let mut e = if let ExprKind::Cast(e, t) = e.kind
104104
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
105105
e
106106
// <expr>.cast_mut()
@@ -112,23 +112,36 @@ fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>)
112112
return None;
113113
};
114114

115-
let e = e.peel_blocks();
116-
117-
// <expr> as *const ...
118-
let e = if let ExprKind::Cast(e, t) = e.kind
119-
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Not, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
120-
e
121-
// ptr::from_ref(<expr>)
122-
} else if let ExprKind::Call(path, [arg]) = e.kind
123-
&& let ExprKind::Path(ref qpath) = path.kind
124-
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
125-
&& cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
126-
arg
127-
} else {
128-
return None;
129-
};
130-
131-
Some(e)
115+
let mut had_at_least_one_cast = false;
116+
loop {
117+
e = e.peel_blocks();
118+
// <expr> as *mut/const ... or <expr> as <uint>
119+
e = if let ExprKind::Cast(expr, t) = e.kind
120+
&& matches!(cx.typeck_results().node_type(t.hir_id).kind(), ty::RawPtr(_) | ty::Uint(_)) {
121+
had_at_least_one_cast = true;
122+
expr
123+
// <expr>.cast(), <expr>.cast_mut() or <expr>.cast_const()
124+
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
125+
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
126+
&& matches!(
127+
cx.tcx.get_diagnostic_name(def_id),
128+
Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut | sym::ptr_cast_const)
129+
)
130+
{
131+
had_at_least_one_cast = true;
132+
expr
133+
// ptr::from_ref(<expr>)
134+
} else if let ExprKind::Call(path, [arg]) = e.kind
135+
&& let ExprKind::Path(ref qpath) = path.kind
136+
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
137+
&& cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
138+
return Some(arg);
139+
} else if had_at_least_one_cast {
140+
return Some(e);
141+
} else {
142+
return None;
143+
};
144+
}
132145
}
133146

134147
fn from_transmute<'tcx>(

compiler/rustc_smir/src/stable_mir/ty.rs

+3
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,14 @@ pub struct UnevaluatedConst {
432432
pub promoted: Option<Promoted>,
433433
}
434434

435+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
435436
pub enum TraitSpecializationKind {
436437
None,
437438
Marker,
438439
AlwaysApplicable,
439440
}
440441

442+
#[derive(Clone, Debug)]
441443
pub struct TraitDecl {
442444
pub def_id: TraitDef,
443445
pub unsafety: Safety,
@@ -454,6 +456,7 @@ pub struct TraitDecl {
454456

455457
pub type ImplTrait = EarlyBinder<TraitRef>;
456458

459+
#[derive(Clone, Debug)]
457460
pub struct TraitRef {
458461
pub def_id: TraitDef,
459462
pub args: GenericArgs,

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ symbols! {
543543
const_panic_fmt,
544544
const_param_ty,
545545
const_precise_live_drops,
546+
const_ptr_cast,
546547
const_raw_ptr_deref,
547548
const_raw_ptr_to_usize_cast,
548549
const_refs_to_cell,
@@ -1160,6 +1161,7 @@ symbols! {
11601161
profiler_runtime,
11611162
ptr,
11621163
ptr_cast,
1164+
ptr_cast_const,
11631165
ptr_cast_mut,
11641166
ptr_const_is_null,
11651167
ptr_from_mut,

library/core/src/ptr/const_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<T: ?Sized> *const T {
5555
/// Casts to a pointer of another type.
5656
#[stable(feature = "ptr_cast", since = "1.38.0")]
5757
#[rustc_const_stable(feature = "const_ptr_cast", since = "1.38.0")]
58+
#[rustc_diagnostic_item = "const_ptr_cast"]
5859
#[inline(always)]
5960
pub const fn cast<U>(self) -> *const U {
6061
self as _

library/core/src/ptr/mut_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl<T: ?Sized> *mut T {
112112
/// [`cast_mut`]: #method.cast_mut
113113
#[stable(feature = "ptr_const_cast", since = "1.65.0")]
114114
#[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
115+
#[rustc_diagnostic_item = "ptr_cast_const"]
115116
#[inline(always)]
116117
pub const fn cast_const(self) -> *const T {
117118
self as _

src/doc/rustc/book.toml

+3
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ title = "The rustc book"
77
git-repository-url = "https://github.com/rust-lang/rust/tree/master/src/doc/rustc"
88
edit-url-template = "https://github.com/rust-lang/rust/edit/master/src/doc/rustc/{path}"
99

10+
[output.html.search]
11+
use-boolean-and = true
12+
1013
[output.html.playground]
1114
runnable = false

src/librustdoc/clean/auto_trait.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ where
4646
let tcx = self.cx.tcx;
4747
let trait_ref = ty::Binder::dummy(ty::TraitRef::new(tcx, trait_def_id, [ty]));
4848
if !self.cx.generated_synthetics.insert((ty, trait_def_id)) {
49-
debug!("get_auto_trait_impl_for({:?}): already generated, aborting", trait_ref);
49+
debug!("get_auto_trait_impl_for({trait_ref:?}): already generated, aborting");
5050
return None;
5151
}
5252

@@ -140,7 +140,7 @@ where
140140
let ty = tcx.type_of(item_def_id).instantiate_identity();
141141
let f = auto_trait::AutoTraitFinder::new(tcx);
142142

143-
debug!("get_auto_trait_impls({:?})", ty);
143+
debug!("get_auto_trait_impls({ty:?})");
144144
let auto_traits: Vec<_> = self.cx.auto_traits.to_vec();
145145
let mut auto_traits: Vec<Item> = auto_traits
146146
.into_iter()
@@ -163,9 +163,9 @@ where
163163
fn get_lifetime(region: Region<'_>, names_map: &FxHashMap<Symbol, Lifetime>) -> Lifetime {
164164
region_name(region)
165165
.map(|name| {
166-
names_map.get(&name).unwrap_or_else(|| {
167-
panic!("Missing lifetime with name {:?} for {:?}", name.as_str(), region)
168-
})
166+
names_map
167+
.get(&name)
168+
.unwrap_or_else(|| panic!("Missing lifetime with name {name:?} for {region:?}"))
169169
})
170170
.unwrap_or(&Lifetime::statik())
171171
.clone()
@@ -372,7 +372,7 @@ where
372372

373373
let output = output.as_ref().cloned().map(Box::new);
374374
if old_output.is_some() && old_output != output {
375-
panic!("Output mismatch for {:?} {:?} {:?}", ty, old_output, output);
375+
panic!("Output mismatch for {ty:?} {old_output:?} {output:?}");
376376
}
377377

378378
let new_params = GenericArgs::Parenthesized { inputs: old_input, output };
@@ -462,7 +462,7 @@ where
462462
);
463463
let mut generic_params = raw_generics.params;
464464

465-
debug!("param_env_to_generics({:?}): generic_params={:?}", item_def_id, generic_params);
465+
debug!("param_env_to_generics({item_def_id:?}): generic_params={generic_params:?}");
466466

467467
let mut has_sized = FxHashSet::default();
468468
let mut ty_to_bounds: FxHashMap<_, FxHashSet<_>> = Default::default();
@@ -623,7 +623,7 @@ where
623623
// loop
624624
ty_to_traits.entry(ty.clone()).or_default().insert(trait_.clone());
625625
}
626-
_ => panic!("Unexpected LHS {:?} for {:?}", lhs, item_def_id),
626+
_ => panic!("Unexpected LHS {lhs:?} for {item_def_id:?}"),
627627
}
628628
}
629629
};
@@ -710,7 +710,7 @@ where
710710
/// involved (impls rarely have more than a few bounds) means that it
711711
/// shouldn't matter in practice.
712712
fn unstable_debug_sort<T: Debug>(&self, vec: &mut [T]) {
713-
vec.sort_by_cached_key(|x| format!("{:?}", x))
713+
vec.sort_by_cached_key(|x| format!("{x:?}"))
714714
}
715715

716716
fn is_fn_trait(&self, path: &Path) -> bool {

src/librustdoc/clean/blanket_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
1717
let param_env = cx.tcx.param_env(item_def_id);
1818
let ty = cx.tcx.type_of(item_def_id);
1919

20-
trace!("get_blanket_impls({:?})", ty);
20+
trace!("get_blanket_impls({ty:?})");
2121
let mut impls = Vec::new();
2222
for trait_def_id in cx.tcx.all_traits() {
2323
if !cx.cache.effective_visibilities.is_reachable(cx.tcx, trait_def_id)
@@ -72,7 +72,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
7272
.into_iter()
7373
.chain(Some(ty::Binder::dummy(impl_trait_ref).to_predicate(infcx.tcx)));
7474
for predicate in predicates {
75-
debug!("testing predicate {:?}", predicate);
75+
debug!("testing predicate {predicate:?}");
7676
let obligation = traits::Obligation::new(
7777
infcx.tcx,
7878
traits::ObligationCause::dummy(),

src/librustdoc/clean/cfg.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ impl<'a> fmt::Display for Display<'a> {
434434
}
435435
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
436436
if self.1.is_html() {
437-
write!(fmt, "<code>{}</code>", feat)?;
437+
write!(fmt, "<code>{feat}</code>")?;
438438
} else {
439-
write!(fmt, "`{}`", feat)?;
439+
write!(fmt, "`{feat}`")?;
440440
}
441441
} else {
442442
write_with_opt_paren(fmt, !sub_cfg.is_all(), Display(sub_cfg, self.1))?;
@@ -471,9 +471,9 @@ impl<'a> fmt::Display for Display<'a> {
471471
}
472472
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
473473
if self.1.is_html() {
474-
write!(fmt, "<code>{}</code>", feat)?;
474+
write!(fmt, "<code>{feat}</code>")?;
475475
} else {
476-
write!(fmt, "`{}`", feat)?;
476+
write!(fmt, "`{feat}`")?;
477477
}
478478
} else {
479479
write_with_opt_paren(fmt, !sub_cfg.is_simple(), Display(sub_cfg, self.1))?;
@@ -552,21 +552,21 @@ impl<'a> fmt::Display for Display<'a> {
552552
"sgx" => "SGX",
553553
_ => "",
554554
},
555-
(sym::target_endian, Some(endian)) => return write!(fmt, "{}-endian", endian),
556-
(sym::target_pointer_width, Some(bits)) => return write!(fmt, "{}-bit", bits),
555+
(sym::target_endian, Some(endian)) => return write!(fmt, "{endian}-endian"),
556+
(sym::target_pointer_width, Some(bits)) => return write!(fmt, "{bits}-bit"),
557557
(sym::target_feature, Some(feat)) => match self.1 {
558558
Format::LongHtml => {
559-
return write!(fmt, "target feature <code>{}</code>", feat);
559+
return write!(fmt, "target feature <code>{feat}</code>");
560560
}
561-
Format::LongPlain => return write!(fmt, "target feature `{}`", feat),
562-
Format::ShortHtml => return write!(fmt, "<code>{}</code>", feat),
561+
Format::LongPlain => return write!(fmt, "target feature `{feat}`"),
562+
Format::ShortHtml => return write!(fmt, "<code>{feat}</code>"),
563563
},
564564
(sym::feature, Some(feat)) => match self.1 {
565565
Format::LongHtml => {
566-
return write!(fmt, "crate feature <code>{}</code>", feat);
566+
return write!(fmt, "crate feature <code>{feat}</code>");
567567
}
568-
Format::LongPlain => return write!(fmt, "crate feature `{}`", feat),
569-
Format::ShortHtml => return write!(fmt, "<code>{}</code>", feat),
568+
Format::LongPlain => return write!(fmt, "crate feature `{feat}`"),
569+
Format::ShortHtml => return write!(fmt, "<code>{feat}</code>"),
570570
},
571571
_ => "",
572572
};
@@ -581,12 +581,12 @@ impl<'a> fmt::Display for Display<'a> {
581581
Escape(v.as_str())
582582
)
583583
} else {
584-
write!(fmt, r#"`{}="{}"`"#, name, v)
584+
write!(fmt, r#"`{name}="{v}"`"#)
585585
}
586586
} else if self.1.is_html() {
587587
write!(fmt, "<code>{}</code>", Escape(name.as_str()))
588588
} else {
589-
write!(fmt, "`{}`", name)
589+
write!(fmt, "`{name}`")
590590
}
591591
}
592592
}

src/librustdoc/clean/inline.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) fn try_inline(
5050
}
5151
let mut ret = Vec::new();
5252

53-
debug!("attrs={:?}", attrs);
53+
debug!("attrs={attrs:?}");
5454

5555
let attrs_without_docs = attrs.map(|(attrs, def_id)| {
5656
(attrs.into_iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>(), def_id)
@@ -529,7 +529,7 @@ pub(crate) fn build_impl(
529529
}
530530

531531
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
532-
trace!("merged_attrs={:?}", merged_attrs);
532+
trace!("merged_attrs={merged_attrs:?}");
533533

534534
trace!(
535535
"build_impl: impl {:?} for {:?}",
@@ -781,7 +781,7 @@ pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
781781
cx.active_extern_traits.insert(did);
782782
}
783783

784-
debug!("record_extern_trait: {:?}", did);
784+
debug!("record_extern_trait: {did:?}");
785785
let trait_ = build_external_trait(cx, did);
786786

787787
cx.external_traits.borrow_mut().insert(did, trait_);

src/librustdoc/clean/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub(crate) fn clean_trait_ref_with_bindings<'tcx>(
215215
) -> Path {
216216
let kind = cx.tcx.def_kind(trait_ref.def_id()).into();
217217
if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
218-
span_bug!(cx.tcx.def_span(trait_ref.def_id()), "`TraitRef` had unexpected kind {:?}", kind);
218+
span_bug!(cx.tcx.def_span(trait_ref.def_id()), "`TraitRef` had unexpected kind {kind:?}");
219219
}
220220
inline::record_extern_fqn(cx, trait_ref.def_id(), kind);
221221
let path =
@@ -304,7 +304,7 @@ pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Life
304304
| ty::ReError(_)
305305
| ty::RePlaceholder(..)
306306
| ty::ReErased => {
307-
debug!("cannot clean region {:?}", region);
307+
debug!("cannot clean region {region:?}");
308308
None
309309
}
310310
}
@@ -1867,11 +1867,11 @@ fn normalize<'tcx>(
18671867
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value));
18681868
match normalized {
18691869
Ok(normalized_value) => {
1870-
debug!("normalized {:?} to {:?}", ty, normalized_value);
1870+
debug!("normalized {ty:?} to {normalized_value:?}");
18711871
Some(normalized_value)
18721872
}
18731873
Err(err) => {
1874-
debug!("failed to normalize {:?}: {:?}", ty, err);
1874+
debug!("failed to normalize {ty:?}: {err:?}");
18751875
None
18761876
}
18771877
}

src/librustdoc/clean/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl ItemId {
7878
#[track_caller]
7979
pub(crate) fn expect_def_id(self) -> DefId {
8080
self.as_def_id()
81-
.unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self))
81+
.unwrap_or_else(|| panic!("ItemId::expect_def_id: `{self:?}` isn't a DefId"))
8282
}
8383

8484
#[inline]
@@ -352,7 +352,7 @@ fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
352352
match tcx.def_kind(parent) {
353353
DefKind::Struct | DefKind::Union => false,
354354
DefKind::Variant => true,
355-
parent_kind => panic!("unexpected parent kind: {:?}", parent_kind),
355+
parent_kind => panic!("unexpected parent kind: {parent_kind:?}"),
356356
}
357357
}
358358

@@ -436,7 +436,7 @@ impl Item {
436436
attrs: Box<Attributes>,
437437
cfg: Option<Arc<Cfg>>,
438438
) -> Item {
439-
trace!("name={:?}, def_id={:?} cfg={:?}", name, def_id, cfg);
439+
trace!("name={name:?}, def_id={def_id:?} cfg={cfg:?}");
440440

441441
Item {
442442
item_id: def_id.into(),

0 commit comments

Comments
 (0)