Skip to content

Commit fb5f32b

Browse files
committed
Auto merge of #164193 - jhpratt:rollup-H0cWA8g, r=jhpratt
Rollup of 5 pull requests Successful merges: - #163981 (New solver opts for `diesel`) - #164088 (const-check: clarify when and how we can rely on non-UB assumptions) - #164105 (Remove unnecessary indirection around `hir::ConstArg`) - #164181 (Tweak output on unused/unaccessible local macro) - #164186 (Update actions/upload-artifact action to v7.0.2)
2 parents b542750 + 63b5b35 commit fb5f32b

36 files changed

Lines changed: 547 additions & 189 deletions

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ jobs:
263263
df -h
264264
265265
- name: upload artifacts to github
266-
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
266+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
267267
with:
268268
# name is set in previous step
269269
name: ${{ env.DOC_ARTIFACT_NAME }}

‎compiler/rustc_ast_lowering/src/lib.rs‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,10 +2844,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
28442844
None,
28452845
);
28462846

2847-
let lowered_args = self.arena.alloc_from_iter(args.iter().map(|arg| {
2848-
let const_arg = self.lower_expr_to_const_arg_direct(arg, None);
2849-
&*self.arena.alloc(const_arg)
2850-
}));
2847+
let lowered_args = self.arena.alloc_from_iter(
2848+
args.iter().map(|arg| self.lower_expr_to_const_arg_direct(arg, None)),
2849+
);
28512850

28522851
ConstArg {
28532852
hir_id: self.lower_node_id(node_id),
@@ -2856,10 +2855,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
28562855
}
28572856
}
28582857
ExprKind::Tup(exprs) => {
2859-
let exprs = self.arena.alloc_from_iter(exprs.iter().map(|expr| {
2860-
let expr = self.lower_expr_to_const_arg_direct(expr, None);
2861-
&*self.arena.alloc(expr)
2862-
}));
2858+
let exprs = self.arena.alloc_from_iter(
2859+
exprs.iter().map(|expr| self.lower_expr_to_const_arg_direct(expr, None)),
2860+
);
28632861

28642862
ConstArg {
28652863
hir_id: self.lower_node_id(node_id),
@@ -2892,12 +2890,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
28922890
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
28932891
let expr = self.lower_expr_to_const_arg_direct(&f.expr, None);
28942892

2895-
&*self.arena.alloc(hir::ConstArgExprField {
2893+
hir::ConstArgExprField {
28962894
hir_id,
28972895
field: self.lower_ident(f.ident),
28982896
expr: self.arena.alloc(expr),
28992897
span: self.lower_span(f.span),
2900-
})
2898+
}
29012899
}));
29022900

29032901
ConstArg {
@@ -2907,10 +2905,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
29072905
}
29082906
}
29092907
ExprKind::Array(elements) => {
2910-
let lowered_elems = self.arena.alloc_from_iter(elements.iter().map(|element| {
2911-
let const_arg = self.lower_expr_to_const_arg_direct(element, None);
2912-
&*self.arena.alloc(const_arg)
2913-
}));
2908+
let lowered_elems = self.arena.alloc_from_iter(
2909+
elements
2910+
.iter()
2911+
.map(|element| self.lower_expr_to_const_arg_direct(element, None)),
2912+
);
29142913
let array_expr = self.arena.alloc(hir::ConstArgArrayExpr {
29152914
span: self.lower_span(expr.span),
29162915
elems: lowered_elems,

‎compiler/rustc_const_eval/src/check_consts/qualifs.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ pub fn in_any_value_of_ty<'tcx>(
4141
/// To accomplish this, const-checking and promotion use a value-based analysis (as opposed to a
4242
/// type-based one). Qualifications propagate structurally across variables: If a local (or a
4343
/// projection of a local) is assigned a qualified value, that local itself becomes qualified.
44+
///
45+
/// Note that whenever this analysis relies on something being UB and hence impossible in
46+
/// well-defined code, it is crucial that const-eval actually detects that UB! Const-eval is not
47+
/// guaranteed to detect all UB, and if it would skip this particular UB we may end up executing
48+
/// code that has not been const-checked.
4449
pub trait Qualif {
4550
/// The name of the file used to debug the dataflow analysis that computes this qualif.
4651
const ANALYSIS_NAME: &'static str;

‎compiler/rustc_const_eval/src/check_consts/resolver.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ where
112112

113113
/// `&` only allow mutation if the borrowed place is `!Freeze`.
114114
///
115-
/// This assumes that it is UB to take the address of a struct field whose type is
116-
/// `Freeze`, then use pointer arithmetic to derive a pointer to a *different* field of
117-
/// that same struct whose type is `!Freeze`. If we decide that this is not UB, we will
118-
/// have to check the type of the borrowed **local** instead of the borrowed **place**
119-
/// below. See [rust-lang/unsafe-code-guidelines#134].
120-
///
121-
/// [rust-lang/unsafe-code-guidelines#134]: https://github.com/rust-lang/unsafe-code-guidelines/issues/134
115+
/// This assumes that const-eval will abort when one creates a shared reference to a struct
116+
/// field whose type is `Freeze`, then uses pointer arithmetic to derive a pointer to a
117+
/// *different* field of that same struct whose type is `!Freeze` and mutates that. It is not
118+
/// enough for that to be UB (which we are pretty sure it is, and it would be a breaking change
119+
/// to make it non-UB as we'd then have to make const checking more conservative here),
120+
/// const-eval also has to reliably detect that UB to ensure we never invoke a destructor after
121+
/// such a mutation occurs!
122122
fn shared_borrow_allows_mutation(&self, place: mir::Place<'tcx>) -> bool {
123123
!place.ty(self.ccx.body, self.ccx.tcx).ty.is_freeze(self.ccx.tcx, self.ccx.typing_env)
124124
}

‎compiler/rustc_const_eval/src/const_eval/machine.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,10 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
11321132
return interp_ok(None);
11331133
}
11341134
// If it's a frozen shared reference that's not already immutable, potentially make it immutable.
1135-
// (Do nothing on `None` provenance, that cannot store immutability anyway.)
1135+
// (Do nothing on `None` provenance, that cannot be used to perform mutation anyway.)
1136+
// Note that const-checking relies on the UB detection we are implementing here! Specifically,
1137+
// the analysis for which destructors may be invoked relies on `&impl Freeze` references not
1138+
// being able to change what gets dropped.
11361139
if let ty::Ref(_, ty, mutbl) = val.layout.ty.kind()
11371140
&& *mutbl == Mutability::Not
11381141
&& val.to_scalar_and_meta().0.to_pointer(ecx).provenance.is_some_and(|p| !p.immutable())

‎compiler/rustc_hir/src/hir.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl<'hir, Unambig> ConstArg<'hir, Unambig> {
508508
#[derive(Clone, Copy, Debug, StableHash)]
509509
#[repr(u8, C)]
510510
pub enum ConstArgKind<'hir, Unambig = ()> {
511-
Tup(&'hir [&'hir ConstArg<'hir>]),
511+
Tup(&'hir [ConstArg<'hir>]),
512512
/// **Note:** Currently this is only used for bare const params
513513
/// (`N` where `fn foo<const N: usize>(...)`),
514514
/// not paths to any const (`N` where `const N: usize = ...`).
@@ -517,9 +517,9 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
517517
Path(QPath<'hir>),
518518
Anon(&'hir AnonConst),
519519
/// Represents construction of struct/struct variants
520-
Struct(QPath<'hir>, &'hir [&'hir ConstArgExprField<'hir>]),
520+
Struct(QPath<'hir>, &'hir [ConstArgExprField<'hir>]),
521521
/// Tuple constructor variant
522-
TupleCall(QPath<'hir>, &'hir [&'hir ConstArg<'hir>]),
522+
TupleCall(QPath<'hir>, &'hir [ConstArg<'hir>]),
523523
/// Array literal argument
524524
Array(&'hir ConstArgArrayExpr<'hir>),
525525
/// Error const
@@ -544,7 +544,7 @@ pub struct ConstArgExprField<'hir> {
544544
#[derive(Clone, Copy, Debug, StableHash)]
545545
pub struct ConstArgArrayExpr<'hir> {
546546
pub span: Span,
547-
pub elems: &'hir [&'hir ConstArg<'hir>],
547+
pub elems: &'hir [ConstArg<'hir>],
548548
}
549549

550550
/// Tracks what a [GenericArg::Infer] can be inferred to based on its syntax.

‎compiler/rustc_hir/src/intravisit.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,13 +1129,13 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
11291129
ConstArgKind::TupleCall(qpath, args) => {
11301130
try_visit!(visitor.visit_qpath(qpath, *hir_id, qpath.span()));
11311131
for arg in *args {
1132-
try_visit!(visitor.visit_const_arg_unambig(*arg));
1132+
try_visit!(visitor.visit_const_arg_unambig(arg));
11331133
}
11341134
V::Result::output()
11351135
}
11361136
ConstArgKind::Array(array_expr) => {
11371137
for arg in array_expr.elems {
1138-
try_visit!(visitor.visit_const_arg_unambig(*arg));
1138+
try_visit!(visitor.visit_const_arg_unambig(arg));
11391139
}
11401140
V::Result::output()
11411141
}

‎compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,7 +2541,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25412541
&self,
25422542
hir_id: HirId,
25432543
qpath: hir::QPath<'_>,
2544-
args: &[&hir::ConstArg<'_>],
2544+
args: &[hir::ConstArg<'_>],
25452545
span: Span,
25462546
) -> Const<'tcx> {
25472547
let tcx = self.tcx();
@@ -2642,7 +2642,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26422642

26432643
fn lower_const_arg_tup(
26442644
&self,
2645-
exprs: &[&hir::ConstArg<'_>],
2645+
exprs: &[hir::ConstArg<'_>],
26462646
ty: Ty<'tcx>,
26472647
span: Span,
26482648
) -> Const<'tcx> {
@@ -2685,7 +2685,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26852685
&self,
26862686
hir_id: HirId,
26872687
qpath: hir::QPath<'_>,
2688-
inits: &[&hir::ConstArgExprField<'_>],
2688+
inits: &[hir::ConstArgExprField<'_>],
26892689
span: Span,
26902690
) -> Const<'tcx> {
26912691
// FIXME(mgca): try to deduplicate this function with

‎compiler/rustc_hir_pretty/src/lib.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,14 +1216,14 @@ impl<'a> State<'a> {
12161216
}
12171217
}
12181218

1219-
fn print_const_struct(&mut self, qpath: &hir::QPath<'_>, fields: &&[&ConstArgExprField<'_>]) {
1219+
fn print_const_struct(&mut self, qpath: &hir::QPath<'_>, fields: &[ConstArgExprField<'_>]) {
12201220
self.print_qpath(qpath, true);
12211221
self.word(" ");
12221222
self.word("{");
12231223
if !fields.is_empty() {
12241224
self.nbsp();
12251225
}
1226-
self.commasep(Inconsistent, *fields, |s, field| {
1226+
self.commasep(Inconsistent, fields, |s, field| {
12271227
s.word(field.field.as_str().to_string());
12281228
s.word(":");
12291229
s.nbsp();
@@ -1232,10 +1232,10 @@ impl<'a> State<'a> {
12321232
self.word("}");
12331233
}
12341234

1235-
fn print_const_ctor(&mut self, qpath: &hir::QPath<'_>, args: &&[&ConstArg<'_, ()>]) {
1235+
fn print_const_ctor(&mut self, qpath: &hir::QPath<'_>, args: &[ConstArg<'_, ()>]) {
12361236
self.print_qpath(qpath, true);
12371237
self.word("(");
1238-
self.commasep(Inconsistent, *args, |s, arg| {
1238+
self.commasep(Inconsistent, args, |s, arg| {
12391239
s.print_const_arg(arg);
12401240
});
12411241
self.word(")");

0 commit comments

Comments
 (0)