Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 722b3ee

Browse files
committed
Auto merge of rust-lang#117498 - matthiaskrgr:rollup-z7mg4ck, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#117298 (Recover from missing param list in function definitions) - rust-lang#117373 (Avoid the path trimming ICE lint in error reporting) - rust-lang#117441 (Do not assert in op_to_const.) - rust-lang#117488 (Update minifier-rs version to 0.3.0) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 75b064d + effc27d commit 722b3ee

25 files changed

+540
-41
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,9 +2465,9 @@ dependencies = [
24652465

24662466
[[package]]
24672467
name = "minifier"
2468-
version = "0.2.3"
2468+
version = "0.3.0"
24692469
source = "registry+https://github.com/rust-lang/crates.io-index"
2470-
checksum = "5394aa376422b4b2b6c02fd9cfcb657e4ec544ae98e43d7d5d785fd0d042fd6d"
2470+
checksum = "95bbbf96b9ac3482c2a25450b67a15ed851319bc5fabf3b40742ea9066e84282"
24712471

24722472
[[package]]
24732473
name = "minimal-lexical"

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,16 @@ pub(crate) fn mk_eval_cx<'mir, 'tcx>(
106106
}
107107

108108
/// This function converts an interpreter value into a MIR constant.
109+
///
110+
/// The `for_diagnostics` flag turns the usual rules for returning `ConstValue::Scalar` into a
111+
/// best-effort attempt. This is not okay for use in const-eval sine it breaks invariants rustc
112+
/// relies on, but it is okay for diagnostics which will just give up gracefully when they
113+
/// encounter an `Indirect` they cannot handle.
109114
#[instrument(skip(ecx), level = "debug")]
110115
pub(super) fn op_to_const<'tcx>(
111116
ecx: &CompileTimeEvalContext<'_, 'tcx>,
112117
op: &OpTy<'tcx>,
118+
for_diagnostics: bool,
113119
) -> ConstValue<'tcx> {
114120
// Handle ZST consistently and early.
115121
if op.layout.is_zst() {
@@ -133,7 +139,13 @@ pub(super) fn op_to_const<'tcx>(
133139
_ => false,
134140
};
135141
let immediate = if force_as_immediate {
136-
Right(ecx.read_immediate(op).expect("normalization works on validated constants"))
142+
match ecx.read_immediate(op) {
143+
Ok(imm) => Right(imm),
144+
Err(err) if !for_diagnostics => {
145+
panic!("normalization works on validated constants: {err:?}")
146+
}
147+
_ => op.as_mplace_or_imm(),
148+
}
137149
} else {
138150
op.as_mplace_or_imm()
139151
};
@@ -205,7 +217,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
205217
);
206218

207219
// Turn this into a proper constant.
208-
op_to_const(&ecx, &mplace.into())
220+
op_to_const(&ecx, &mplace.into(), /* for diagnostics */ false)
209221
}
210222

211223
#[instrument(skip(tcx), level = "debug")]

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn eval_to_valtree<'tcx>(
7272
}
7373

7474
#[instrument(skip(tcx), level = "debug")]
75-
pub(crate) fn try_destructure_mir_constant_for_diagnostics<'tcx>(
75+
pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
7676
tcx: TyCtxtAt<'tcx>,
7777
val: mir::ConstValue<'tcx>,
7878
ty: Ty<'tcx>,
@@ -99,7 +99,7 @@ pub(crate) fn try_destructure_mir_constant_for_diagnostics<'tcx>(
9999
let fields_iter = (0..field_count)
100100
.map(|i| {
101101
let field_op = ecx.project_field(&down, i).ok()?;
102-
let val = op_to_const(&ecx, &field_op);
102+
let val = op_to_const(&ecx, &field_op, /* for diagnostics */ true);
103103
Some((val, field_op.layout.ty))
104104
})
105105
.collect::<Option<Vec<_>>>()?;

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub fn valtree_to_const_value<'tcx>(
232232
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No);
233233
let imm = valtree_to_ref(&mut ecx, valtree, *inner_ty);
234234
let imm = ImmTy::from_immediate(imm, tcx.layout_of(param_env_ty).unwrap());
235-
op_to_const(&ecx, &imm.into())
235+
op_to_const(&ecx, &imm.into(), /* for diagnostics */ false)
236236
}
237237
ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => {
238238
let layout = tcx.layout_of(param_env_ty).unwrap();
@@ -265,7 +265,7 @@ pub fn valtree_to_const_value<'tcx>(
265265
dump_place(&ecx, &place);
266266
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &place).unwrap();
267267

268-
op_to_const(&ecx, &place.into())
268+
op_to_const(&ecx, &place.into(), /* for diagnostics */ false)
269269
}
270270
ty::Never
271271
| ty::Error(_)

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
145145
assert!(dest.layout.is_sized());
146146
assert_eq!(cast_ty, dest.layout.ty); // we otherwise ignore `cast_ty` enirely...
147147
if src.layout.size != dest.layout.size {
148-
let src_bytes = src.layout.size.bytes();
149-
let dest_bytes = dest.layout.size.bytes();
150-
let src_ty = format!("{}", src.layout.ty);
151-
let dest_ty = format!("{}", dest.layout.ty);
152148
throw_ub_custom!(
153149
fluent::const_eval_invalid_transmute,
154-
src_bytes = src_bytes,
155-
dest_bytes = dest_bytes,
156-
src = src_ty,
157-
dest = dest_ty,
150+
src_bytes = src.layout.size.bytes(),
151+
dest_bytes = dest.layout.size.bytes(),
152+
src = src.layout.ty,
153+
dest = dest.layout.ty,
158154
);
159155
}
160156

compiler/rustc_const_eval/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub fn provide(providers: &mut Providers) {
5454
let (param_env, raw) = param_env_and_value.into_parts();
5555
const_eval::eval_to_valtree(tcx, param_env, raw)
5656
};
57-
providers.hooks.try_destructure_mir_constant_for_diagnostics =
58-
const_eval::try_destructure_mir_constant_for_diagnostics;
57+
providers.hooks.try_destructure_mir_constant_for_user_output =
58+
const_eval::try_destructure_mir_constant_for_user_output;
5959
providers.valtree_to_const_val = |tcx, (ty, valtree)| {
6060
const_eval::valtree_to_const_value(tcx, ty::ParamEnv::empty().and(ty), valtree)
6161
};

compiler/rustc_middle/src/hooks/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ macro_rules! declare_hooks {
6666
declare_hooks! {
6767
/// Tries to destructure an `mir::Const` ADT or array into its variant index
6868
/// and its field values. This should only be used for pretty printing.
69-
hook try_destructure_mir_constant_for_diagnostics(val: mir::ConstValue<'tcx>, ty: Ty<'tcx>) -> Option<mir::DestructuredConstant<'tcx>>;
69+
hook try_destructure_mir_constant_for_user_output(val: mir::ConstValue<'tcx>, ty: Ty<'tcx>) -> Option<mir::DestructuredConstant<'tcx>>;
7070

7171
/// Getting a &core::panic::Location referring to a span.
7272
hook const_caller_location(file: rustc_span::Symbol, line: u32, col: u32) -> mir::ConstValue<'tcx>;

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ fn pretty_print_const_value_tcx<'tcx>(
17131713
(_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_non_region_param() => {
17141714
let ct = tcx.lift(ct).unwrap();
17151715
let ty = tcx.lift(ty).unwrap();
1716-
if let Some(contents) = tcx.try_destructure_mir_constant_for_diagnostics(ct, ty) {
1716+
if let Some(contents) = tcx.try_destructure_mir_constant_for_user_output(ct, ty) {
17171717
let fields: Vec<(ConstValue<'_>, Ty<'_>)> = contents.fields.to_vec();
17181718
match *ty.kind() {
17191719
ty::Array(..) => {

compiler/rustc_parse/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ parse_missing_fn_for_function_definition = missing `fn` for function definition
526526
parse_missing_fn_for_method_definition = missing `fn` for method definition
527527
.suggestion = add `fn` here to parse `{$ident}` as a public method
528528
529+
parse_missing_fn_params = missing parameters for function definition
530+
.suggestion = add a parameter list
531+
529532
parse_missing_for_in_trait_impl = missing `for` in a trait impl
530533
.suggestion = add `for` here
531534

compiler/rustc_parse/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,14 @@ pub(crate) enum AmbiguousMissingKwForItemSub {
15511551
HelpMacro,
15521552
}
15531553

1554+
#[derive(Diagnostic)]
1555+
#[diag(parse_missing_fn_params)]
1556+
pub(crate) struct MissingFnParams {
1557+
#[primary_span]
1558+
#[suggestion(code = "()", applicability = "machine-applicable", style = "short")]
1559+
pub span: Span,
1560+
}
1561+
15541562
#[derive(Diagnostic)]
15551563
#[diag(parse_missing_trait_in_trait_impl)]
15561564
pub(crate) struct MissingTraitInTraitImpl {

0 commit comments

Comments
 (0)