Skip to content

Commit 126dcc6

Browse files
committed
Use less fragile error handling
1 parent 77fe9f0 commit 126dcc6

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,16 @@ fn eval_body_using_ecx<'mir, 'tcx, R: InterpretationResult<'tcx>>(
9898
// Only report this after validation, as validaiton produces much better diagnostics.
9999
// FIXME: ensure validation always reports this and stop making interning care about it.
100100

101-
if let Err(InternResult { found_bad_mutable_pointer, found_dangling_pointer }) = intern_result {
102-
if found_dangling_pointer {
101+
match intern_result {
102+
Ok(()) => {}
103+
Err(InternResult::FoundDanglingPointer) => {
103104
return Err(ecx
104105
.tcx
105106
.dcx()
106107
.emit_err(DanglingPtrInFinal { span: ecx.tcx.span, kind: intern_kind })
107108
.into());
108-
} else if found_bad_mutable_pointer {
109+
}
110+
Err(InternResult::FoundBadMutablePointer) => {
109111
// only report mutable pointers if there were no dangling pointers
110112
let err_diag = errors::MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind };
111113
ecx.tcx.emit_node_span_lint(

compiler/rustc_const_eval/src/interpret/intern.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,10 @@ pub enum InternKind {
132132
Promoted,
133133
}
134134

135-
#[derive(Default, Debug)]
136-
pub struct InternResult {
137-
pub found_bad_mutable_pointer: bool,
138-
pub found_dangling_pointer: bool,
139-
}
140-
141-
impl InternResult {
142-
fn has_errors(&self) -> bool {
143-
let Self { found_bad_mutable_pointer, found_dangling_pointer } = *self;
144-
found_bad_mutable_pointer || found_dangling_pointer
145-
}
135+
#[derive(Debug)]
136+
pub enum InternResult {
137+
FoundBadMutablePointer,
138+
FoundDanglingPointer,
146139
}
147140

148141
/// Intern `ret` and everything it references.
@@ -212,7 +205,7 @@ pub fn intern_const_alloc_recursive<
212205
// Whether we encountered a bad mutable pointer.
213206
// We want to first report "dangling" and then "mutable", so we need to delay reporting these
214207
// errors.
215-
let mut result = InternResult::default();
208+
let mut result = Ok(());
216209

217210
// Keep interning as long as there are things to intern.
218211
// We show errors if there are dangling pointers, or mutable pointers in immutable contexts
@@ -262,7 +255,10 @@ pub fn intern_const_alloc_recursive<
262255
// on the promotion analysis not screwing up to ensure that it is sound to intern
263256
// promoteds as immutable.
264257
trace!("found bad mutable pointer");
265-
result.found_bad_mutable_pointer = true;
258+
// Prefer dangling pointer errors over mutable pointer errors
259+
if result.is_ok() {
260+
result = Err(InternResult::FoundBadMutablePointer);
261+
}
266262
}
267263
if ecx.tcx.try_get_global_alloc(alloc_id).is_some() {
268264
// Already interned.
@@ -284,11 +280,11 @@ pub fn intern_const_alloc_recursive<
284280
Ok(nested) => todo.extend(nested),
285281
Err(()) => {
286282
ecx.tcx.dcx().delayed_bug("found dangling pointer during const interning");
287-
result.found_dangling_pointer = true
283+
result = Err(InternResult::FoundDanglingPointer);
288284
}
289285
}
290286
}
291-
if result.has_errors() { Err(result) } else { Ok(()) }
287+
result
292288
}
293289

294290
/// Intern `ret`. This function assumes that `ret` references no other allocation.

0 commit comments

Comments
 (0)