Skip to content

Commit 4459e72

Browse files
committed
Auto merge of #91656 - matthiaskrgr:rollup-lk96y6d, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #83744 (Deprecate crate_type and crate_name nested inside #![cfg_attr]) - #90550 (Update certificates in some Ubuntu 16 images.) - #91272 (Print a suggestion when comparing references to primitive types in `const fn`) - #91467 (Emphasise that an OsStr[ing] is not necessarily a platform string) - #91531 (Do not add `;` to expected tokens list when it's wrong) - #91577 (Address some FIXMEs left over from #91475) - #91638 (Remove `in_band_lifetimes` from `rustc_mir_transform`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents ce0f7ba + 90690da commit 4459e72

File tree

70 files changed

+624
-219
lines changed

Some content is hidden

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

70 files changed

+624
-219
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
801801
if let Some(trait_id) = tcx.trait_of_item(callee) {
802802
trace!("attempting to call a trait method");
803803
if !self.tcx.features().const_trait_impl {
804-
self.check_op(ops::FnCallNonConst);
804+
self.check_op(ops::FnCallNonConst(Some((callee, substs))));
805805
return;
806806
}
807807

@@ -857,7 +857,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
857857
}
858858

859859
if !nonconst_call_permission {
860-
self.check_op(ops::FnCallNonConst);
860+
self.check_op(ops::FnCallNonConst(None));
861861
return;
862862
}
863863
}
@@ -926,7 +926,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
926926
}
927927

928928
if !nonconst_call_permission {
929-
self.check_op(ops::FnCallNonConst);
929+
self.check_op(ops::FnCallNonConst(None));
930930
return;
931931
}
932932
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+63-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
3-
use rustc_errors::{struct_span_err, DiagnosticBuilder};
3+
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::DefId;
6-
use rustc_middle::mir;
6+
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
7+
use rustc_middle::{mir, ty::AssocKind};
78
use rustc_session::parse::feature_err;
89
use rustc_span::symbol::sym;
9-
use rustc_span::{Span, Symbol};
10+
use rustc_span::{symbol::Ident, Span, Symbol};
11+
use rustc_span::{BytePos, Pos};
1012

1113
use super::ConstCx;
1214

@@ -72,17 +74,71 @@ impl NonConstOp for FnCallIndirect {
7274

7375
/// A function call where the callee is not marked as `const`.
7476
#[derive(Debug)]
75-
pub struct FnCallNonConst;
76-
impl NonConstOp for FnCallNonConst {
77+
pub struct FnCallNonConst<'tcx>(pub Option<(DefId, SubstsRef<'tcx>)>);
78+
impl<'a> NonConstOp for FnCallNonConst<'a> {
7779
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
78-
struct_span_err!(
80+
let mut err = struct_span_err!(
7981
ccx.tcx.sess,
8082
span,
8183
E0015,
8284
"calls in {}s are limited to constant functions, \
8385
tuple structs and tuple variants",
8486
ccx.const_kind(),
85-
)
87+
);
88+
89+
if let FnCallNonConst(Some((callee, substs))) = *self {
90+
if let Some(trait_def_id) = ccx.tcx.lang_items().eq_trait() {
91+
if let Some(eq_item) = ccx.tcx.associated_items(trait_def_id).find_by_name_and_kind(
92+
ccx.tcx,
93+
Ident::with_dummy_span(sym::eq),
94+
AssocKind::Fn,
95+
trait_def_id,
96+
) {
97+
if callee == eq_item.def_id && substs.len() == 2 {
98+
match (substs[0].unpack(), substs[1].unpack()) {
99+
(GenericArgKind::Type(self_ty), GenericArgKind::Type(rhs_ty))
100+
if self_ty == rhs_ty
101+
&& self_ty.is_ref()
102+
&& self_ty.peel_refs().is_primitive() =>
103+
{
104+
let mut num_refs = 0;
105+
let mut tmp_ty = self_ty;
106+
while let rustc_middle::ty::Ref(_, inner_ty, _) = tmp_ty.kind() {
107+
num_refs += 1;
108+
tmp_ty = inner_ty;
109+
}
110+
let deref = "*".repeat(num_refs);
111+
112+
if let Ok(call_str) =
113+
ccx.tcx.sess.source_map().span_to_snippet(span)
114+
{
115+
if let Some(eq_idx) = call_str.find("==") {
116+
if let Some(rhs_idx) = call_str[(eq_idx + 2)..]
117+
.find(|c: char| !c.is_whitespace())
118+
{
119+
let rhs_pos = span.lo()
120+
+ BytePos::from_usize(eq_idx + 2 + rhs_idx);
121+
let rhs_span = span.with_lo(rhs_pos).with_hi(rhs_pos);
122+
err.multipart_suggestion(
123+
"consider dereferencing here",
124+
vec![
125+
(span.shrink_to_lo(), deref.clone()),
126+
(rhs_span, deref),
127+
],
128+
Applicability::MachineApplicable,
129+
);
130+
}
131+
}
132+
}
133+
}
134+
_ => {}
135+
}
136+
}
137+
}
138+
}
139+
}
140+
141+
err
86142
}
87143
}
88144

compiler/rustc_expand/src/config.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,24 @@ impl<'a> StripUnconfigured<'a> {
402402
);
403403
trees.push((bracket_group, Spacing::Alone));
404404
let tokens = Some(LazyTokenStream::new(AttrAnnotatedTokenStream::new(trees)));
405-
self.process_cfg_attr(attr::mk_attr_from_item(item, tokens, attr.style, span))
405+
let attr = attr::mk_attr_from_item(item, tokens, attr.style, span);
406+
if attr.has_name(sym::crate_type) {
407+
self.sess.parse_sess.buffer_lint(
408+
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
409+
attr.span,
410+
ast::CRATE_NODE_ID,
411+
"`crate_type` within an `#![cfg_attr] attribute is deprecated`",
412+
);
413+
}
414+
if attr.has_name(sym::crate_name) {
415+
self.sess.parse_sess.buffer_lint(
416+
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
417+
attr.span,
418+
ast::CRATE_NODE_ID,
419+
"`crate_name` within an `#![cfg_attr] attribute is deprecated`",
420+
);
421+
}
422+
self.process_cfg_attr(attr)
406423
})
407424
.collect()
408425
}

compiler/rustc_lint_defs/src/builtin.rs

+36
Original file line numberDiff line numberDiff line change
@@ -2960,6 +2960,41 @@ declare_lint! {
29602960
"detects large moves or copies",
29612961
}
29622962

2963+
declare_lint! {
2964+
/// The `deprecated_cfg_attr_crate_type_name` lint detects uses of the
2965+
/// `#![cfg_attr(..., crate_type = "...")]` and
2966+
/// `#![cfg_attr(..., crate_name = "...")]` attributes to conditionally
2967+
/// specify the crate type and name in the source code.
2968+
///
2969+
/// ### Example
2970+
///
2971+
/// ```rust
2972+
/// #![cfg_attr(debug_assertions, crate_type = "lib")]
2973+
/// ```
2974+
///
2975+
/// {{produces}}
2976+
///
2977+
///
2978+
/// ### Explanation
2979+
///
2980+
/// The `#![crate_type]` and `#![crate_name]` attributes require a hack in
2981+
/// the compiler to be able to change the used crate type and crate name
2982+
/// after macros have been expanded. Neither attribute works in combination
2983+
/// with Cargo as it explicitly passes `--crate-type` and `--crate-name` on
2984+
/// the commandline. These values must match the value used in the source
2985+
/// code to prevent an error.
2986+
///
2987+
/// To fix the warning use `--crate-type` on the commandline when running
2988+
/// rustc instead of `#![cfg_attr(..., crate_type = "...")]` and
2989+
/// `--crate-name` instead of `#![cfg_attr(..., crate_name = "...")]`.
2990+
pub DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
2991+
Warn,
2992+
"detects usage of `#![cfg_attr(..., crate_type/crate_name = \"...\")]`",
2993+
@future_incompatible = FutureIncompatibleInfo {
2994+
reference: "issue #91632 <https://github.com/rust-lang/rust/issues/91632>",
2995+
};
2996+
}
2997+
29632998
declare_lint_pass! {
29642999
/// Does nothing as a lint pass, but registers some `Lint`s
29653000
/// that are used by other parts of the compiler.
@@ -3056,6 +3091,7 @@ declare_lint_pass! {
30563091
NON_EXHAUSTIVE_OMITTED_PATTERNS,
30573092
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
30583093
DEREF_INTO_DYN_SUPERTRAIT,
3094+
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
30593095
]
30603096
}
30613097

compiler/rustc_mir_transform/src/add_retag.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn is_stable(place: PlaceRef<'_>) -> bool {
3434
}
3535

3636
/// Determine whether this type may be a reference (or box), and thus needs retagging.
37-
fn may_be_reference(ty: Ty<'tcx>) -> bool {
37+
fn may_be_reference(ty: Ty<'_>) -> bool {
3838
match ty.kind() {
3939
// Primitive types that are not references
4040
ty::Bool

compiler/rustc_mir_transform/src/check_const_item_mutation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct ConstMutationChecker<'a, 'tcx> {
2323
target_local: Option<Local>,
2424
}
2525

26-
impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> {
26+
impl<'tcx> ConstMutationChecker<'_, 'tcx> {
2727
fn is_const_item(&self, local: Local) -> Option<DefId> {
2828
if let Some(box LocalInfo::ConstRef { def_id }) = self.body.local_decls[local].local_info {
2929
Some(def_id)
@@ -95,7 +95,7 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> {
9595
}
9696
}
9797

98-
impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> {
98+
impl<'tcx> Visitor<'tcx> for ConstMutationChecker<'_, 'tcx> {
9999
fn visit_statement(&mut self, stmt: &Statement<'tcx>, loc: Location) {
100100
if let StatementKind::Assign(box (lhs, _)) = &stmt.kind {
101101
// Check for assignment to fields of a constant

compiler/rustc_mir_transform/src/check_packed_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn builtin_derive_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
6666
}
6767
}
6868

69-
impl<'a, 'tcx> Visitor<'tcx> for PackedRefChecker<'a, 'tcx> {
69+
impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
7070
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
7171
// Make sure we know where in the MIR we are.
7272
self.source_info = terminator.source_info;

compiler/rustc_mir_transform/src/check_unsafety.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
4646
}
4747
}
4848

49-
impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
49+
impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
5050
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
5151
self.source_info = terminator.source_info;
5252
match terminator.kind {
@@ -244,7 +244,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
244244
}
245245
}
246246

247-
impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
247+
impl<'tcx> UnsafetyChecker<'_, 'tcx> {
248248
fn require_unsafe(&mut self, kind: UnsafetyViolationKind, details: UnsafetyViolationDetails) {
249249
// Violations can turn out to be `UnsafeFn` during analysis, but they should not start out as such.
250250
assert_ne!(kind, UnsafetyViolationKind::UnsafeFn);
@@ -397,7 +397,7 @@ struct UnusedUnsafeVisitor<'a> {
397397
unsafe_blocks: &'a mut Vec<(hir::HirId, bool)>,
398398
}
399399

400-
impl<'a, 'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'a> {
400+
impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_> {
401401
type Map = intravisit::ErasedMap<'tcx>;
402402

403403
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {

compiler/rustc_mir_transform/src/const_debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn find_optimization_oportunities<'tcx>(body: &Body<'tcx>) -> Vec<(Local, Consta
8989
eligable_locals
9090
}
9191

92-
impl<'tcx> Visitor<'tcx> for LocalUseVisitor {
92+
impl Visitor<'_> for LocalUseVisitor {
9393
fn visit_local(&mut self, local: &Local, context: PlaceContext, location: Location) {
9494
if context.is_mutating_use() {
9595
self.local_mutating_uses[*local] = self.local_mutating_uses[*local].saturating_add(1);

compiler/rustc_mir_transform/src/const_goto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'tcx> MirPass<'tcx> for ConstGoto {
5454
}
5555
}
5656

57-
impl<'a, 'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'a, 'tcx> {
57+
impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
5858
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
5959
let _: Option<_> = try {
6060
let target = terminator.kind.as_goto()?;

compiler/rustc_mir_transform/src/const_prop.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ struct ConstPropMachine<'mir, 'tcx> {
171171
can_const_prop: IndexVec<Local, ConstPropMode>,
172172
}
173173

174-
impl<'mir, 'tcx> ConstPropMachine<'mir, 'tcx> {
174+
impl ConstPropMachine<'_, '_> {
175175
fn new(
176176
only_propagate_inside_block_locals: BitSet<Local>,
177177
can_const_prop: IndexVec<Local, ConstPropMode>,
@@ -308,14 +308,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
308308
}
309309

310310
#[inline(always)]
311-
fn stack(
311+
fn stack<'a>(
312312
ecx: &'a InterpCx<'mir, 'tcx, Self>,
313313
) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
314314
&ecx.machine.stack
315315
}
316316

317317
#[inline(always)]
318-
fn stack_mut(
318+
fn stack_mut<'a>(
319319
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
320320
) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
321321
&mut ecx.machine.stack
@@ -336,7 +336,7 @@ struct ConstPropagator<'mir, 'tcx> {
336336
source_info: Option<SourceInfo>,
337337
}
338338

339-
impl<'mir, 'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'mir, 'tcx> {
339+
impl<'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'_, 'tcx> {
340340
type LayoutOfResult = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
341341

342342
#[inline]
@@ -345,21 +345,21 @@ impl<'mir, 'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'mir, 'tcx> {
345345
}
346346
}
347347

348-
impl<'mir, 'tcx> HasDataLayout for ConstPropagator<'mir, 'tcx> {
348+
impl HasDataLayout for ConstPropagator<'_, '_> {
349349
#[inline]
350350
fn data_layout(&self) -> &TargetDataLayout {
351351
&self.tcx.data_layout
352352
}
353353
}
354354

355-
impl<'mir, 'tcx> ty::layout::HasTyCtxt<'tcx> for ConstPropagator<'mir, 'tcx> {
355+
impl<'tcx> ty::layout::HasTyCtxt<'tcx> for ConstPropagator<'_, 'tcx> {
356356
#[inline]
357357
fn tcx(&self) -> TyCtxt<'tcx> {
358358
self.tcx
359359
}
360360
}
361361

362-
impl<'mir, 'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'mir, 'tcx> {
362+
impl<'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'_, 'tcx> {
363363
#[inline]
364364
fn param_env(&self) -> ty::ParamEnv<'tcx> {
365365
self.param_env
@@ -971,7 +971,7 @@ struct CanConstProp {
971971

972972
impl CanConstProp {
973973
/// Returns true if `local` can be propagated
974-
fn check(
974+
fn check<'tcx>(
975975
tcx: TyCtxt<'tcx>,
976976
param_env: ParamEnv<'tcx>,
977977
body: &Body<'tcx>,
@@ -1019,7 +1019,7 @@ impl CanConstProp {
10191019
}
10201020
}
10211021

1022-
impl<'tcx> Visitor<'tcx> for CanConstProp {
1022+
impl Visitor<'_> for CanConstProp {
10231023
fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
10241024
use rustc_middle::mir::visit::PlaceContext::*;
10251025
match context {
@@ -1079,7 +1079,7 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
10791079
}
10801080
}
10811081

1082-
impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
1082+
impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
10831083
fn tcx(&self) -> TyCtxt<'tcx> {
10841084
self.tcx
10851085
}

0 commit comments

Comments
 (0)