Skip to content

Commit 8af9c26

Browse files
committed
Use less fragile error handling
1 parent 6290e5f commit 8af9c26

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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,10 @@ pub enum InternKind {
122122
Promoted,
123123
}
124124

125-
#[derive(Default, Debug)]
126-
pub struct InternResult {
127-
pub found_bad_mutable_pointer: bool,
128-
pub found_dangling_pointer: bool,
129-
}
130-
131-
impl InternResult {
132-
fn has_errors(&self) -> bool {
133-
let Self { found_bad_mutable_pointer, found_dangling_pointer } = *self;
134-
found_bad_mutable_pointer || found_dangling_pointer
135-
}
125+
#[derive(Debug)]
126+
pub enum InternResult {
127+
FoundBadMutablePointer,
128+
FoundDanglingPointer,
136129
}
137130

138131
/// Intern `ret` and everything it references.
@@ -202,7 +195,7 @@ pub fn intern_const_alloc_recursive<
202195
// Whether we encountered a bad mutable pointer.
203196
// We want to first report "dangling" and then "mutable", so we need to delay reporting these
204197
// errors.
205-
let mut result = InternResult::default();
198+
let mut result = Ok(());
206199

207200
// Keep interning as long as there are things to intern.
208201
// We show errors if there are dangling pointers, or mutable pointers in immutable contexts
@@ -252,7 +245,10 @@ pub fn intern_const_alloc_recursive<
252245
// on the promotion analysis not screwing up to ensure that it is sound to intern
253246
// promoteds as immutable.
254247
trace!("found bad mutable pointer");
255-
result.found_bad_mutable_pointer = true;
248+
// Prefer dangling pointer errors over mutable pointer errors
249+
if result.is_ok() {
250+
result = Err(InternResult::FoundBadMutablePointer);
251+
}
256252
}
257253
if ecx.tcx.try_get_global_alloc(alloc_id).is_some() {
258254
// Already interned.
@@ -274,11 +270,11 @@ pub fn intern_const_alloc_recursive<
274270
Ok(nested) => todo.extend(nested),
275271
Err(()) => {
276272
ecx.tcx.dcx().delayed_bug("found dangling pointer during const interning");
277-
result.found_dangling_pointer = true
273+
result = Err(InternResult::FoundDanglingPointer);
278274
}
279275
}
280276
}
281-
if result.has_errors() { Err(result) } else { Ok(()) }
277+
result
282278
}
283279

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

0 commit comments

Comments
 (0)