Skip to content

Commit b05c790

Browse files
committed
Reuse the ctfe error emitting logic for the future incompat lint
1 parent dec05e9 commit b05c790

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::def::DefKind;
22
use rustc_hir::{LangItem, CRATE_HIR_ID};
33
use rustc_middle::mir;
4-
use rustc_middle::mir::interpret::{PointerArithmetic, UndefinedBehaviorInfo};
4+
use rustc_middle::mir::interpret::{InterpError, PointerArithmetic, UndefinedBehaviorInfo};
55
use rustc_middle::ty::layout::FnAbiOf;
66
use rustc_middle::ty::{self, Ty, TyCtxt};
77
use rustc_session::lint::builtin::INVALID_ALIGNMENT;
@@ -353,22 +353,27 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
353353
ecx.cur_span(),
354354
"`alignment_check_failed` called when no alignment check requested"
355355
),
356-
CheckAlignment::FutureIncompat => ecx.tcx.struct_span_lint_hir(
357-
INVALID_ALIGNMENT,
358-
ecx.stack().iter().find_map(|frame| frame.lint_root()).unwrap_or(CRATE_HIR_ID),
359-
ecx.cur_span(),
360-
UndefinedBehaviorInfo::AlignmentCheckFailed { has, required }.to_string(),
361-
|db| {
362-
let mut stacktrace = ecx.generate_stacktrace();
363-
// Filter out `requires_caller_location` frames.
364-
stacktrace
365-
.retain(|frame| !frame.instance.def.requires_caller_location(*ecx.tcx));
366-
for frame in stacktrace {
367-
db.span_label(frame.span, format!("inside `{}`", frame.instance));
368-
}
369-
db
370-
},
371-
),
356+
CheckAlignment::FutureIncompat => {
357+
let err = ConstEvalErr::new(
358+
ecx,
359+
InterpError::UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed {
360+
has,
361+
required,
362+
})
363+
.into(),
364+
None,
365+
);
366+
ecx.tcx.struct_span_lint_hir(
367+
INVALID_ALIGNMENT,
368+
ecx.stack().iter().find_map(|frame| frame.lint_root()).unwrap_or(CRATE_HIR_ID),
369+
err.span,
370+
err.error.to_string(),
371+
|db| {
372+
err.decorate(db, |_| {});
373+
db
374+
},
375+
);
376+
}
372377
}
373378
Ok(())
374379
}

src/test/ui/consts/const-eval/ub-ref-ptr.32bit.stderr

+16-18
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,17 @@ LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
151151
error: accessing memory with alignment 1, but alignment 4 is required
152152
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
153153
|
154-
= note: inside `std::ptr::read::<u32>`
154+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
155+
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
156+
note: inside `std::ptr::read::<u32>`
157+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
158+
note: inside `ptr::const_ptr::<impl *const u32>::read`
155159
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
156-
|
157-
= note: inside `ptr::const_ptr::<impl *const u32>::read`
158-
|
159-
::: $DIR/ub-ref-ptr.rs:65:5
160+
note: inside `UNALIGNED_READ`
161+
--> $DIR/ub-ref-ptr.rs:65:5
160162
|
161163
LL | ptr.read();
162-
| ---------- inside `UNALIGNED_READ`
163-
|
164-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
165-
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
164+
| ^^^^^^^^^^
166165
= note: `#[deny(invalid_alignment)]` on by default
167166

168167
error: aborting due to 15 previous errors
@@ -172,17 +171,16 @@ Future incompatibility report: Future breakage diagnostic:
172171
error: accessing memory with alignment 1, but alignment 4 is required
173172
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
174173
|
175-
= note: inside `std::ptr::read::<u32>`
174+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
175+
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
176+
note: inside `std::ptr::read::<u32>`
177+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
178+
note: inside `ptr::const_ptr::<impl *const u32>::read`
176179
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
177-
|
178-
= note: inside `ptr::const_ptr::<impl *const u32>::read`
179-
|
180-
::: $DIR/ub-ref-ptr.rs:65:5
180+
note: inside `UNALIGNED_READ`
181+
--> $DIR/ub-ref-ptr.rs:65:5
181182
|
182183
LL | ptr.read();
183-
| ---------- inside `UNALIGNED_READ`
184-
|
185-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
186-
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
184+
| ^^^^^^^^^^
187185
= note: `#[deny(invalid_alignment)]` on by default
188186

src/test/ui/consts/const-eval/ub-ref-ptr.64bit.stderr

+16-18
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,17 @@ LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
151151
error: accessing memory with alignment 1, but alignment 4 is required
152152
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
153153
|
154-
= note: inside `std::ptr::read::<u32>`
154+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
155+
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
156+
note: inside `std::ptr::read::<u32>`
157+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
158+
note: inside `ptr::const_ptr::<impl *const u32>::read`
155159
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
156-
|
157-
= note: inside `ptr::const_ptr::<impl *const u32>::read`
158-
|
159-
::: $DIR/ub-ref-ptr.rs:65:5
160+
note: inside `UNALIGNED_READ`
161+
--> $DIR/ub-ref-ptr.rs:65:5
160162
|
161163
LL | ptr.read();
162-
| ---------- inside `UNALIGNED_READ`
163-
|
164-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
165-
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
164+
| ^^^^^^^^^^
166165
= note: `#[deny(invalid_alignment)]` on by default
167166

168167
error: aborting due to 15 previous errors
@@ -172,17 +171,16 @@ Future incompatibility report: Future breakage diagnostic:
172171
error: accessing memory with alignment 1, but alignment 4 is required
173172
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
174173
|
175-
= note: inside `std::ptr::read::<u32>`
174+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
175+
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
176+
note: inside `std::ptr::read::<u32>`
177+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
178+
note: inside `ptr::const_ptr::<impl *const u32>::read`
176179
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
177-
|
178-
= note: inside `ptr::const_ptr::<impl *const u32>::read`
179-
|
180-
::: $DIR/ub-ref-ptr.rs:65:5
180+
note: inside `UNALIGNED_READ`
181+
--> $DIR/ub-ref-ptr.rs:65:5
181182
|
182183
LL | ptr.read();
183-
| ---------- inside `UNALIGNED_READ`
184-
|
185-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
186-
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616>
184+
| ^^^^^^^^^^
187185
= note: `#[deny(invalid_alignment)]` on by default
188186

0 commit comments

Comments
 (0)