Skip to content

Commit

Permalink
compiler: Tighten up ImproperCTypesLayer recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee authored and niacdoial committed Dec 6, 2024
1 parent 8b6289f commit 02072fd
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,19 +1593,16 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
}]);
}
ffir @ FfiResult::FfiUnsafeWrapper { .. } => {
let mut last_ty = None;
let mut ffiresult_recursor = Some(&ffir);
let mut ffiresult_recursor = ControlFlow::Continue(&ffir);
let mut cimproper_layers: Vec<ImproperCTypesLayer<'tcx>> = vec![];

// this whole while block converts the arbitrarily-deep
// FfiResult stack to a ImproperCTypesLayer Vec
while let Some(ref ffir_rec) = ffiresult_recursor {
// FfiResult stack to an ImproperCTypesLayer Vec
while let ControlFlow::Continue(ref ffir_rec) = ffiresult_recursor {
match ffir_rec {
FfiResult::FfiPhantom(ty) => {
last_ty = Some(ty.clone());
let len = cimproper_layers.len();
if len > 0 {
cimproper_layers[len - 1].inner_ty = last_ty.clone();
if let Some(layer) = cimproper_layers.last_mut() {
layer.inner_ty = Some(ty.clone());
}
cimproper_layers.push(ImproperCTypesLayer {
ty: ty.clone(),
Expand All @@ -1614,14 +1611,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
note: fluent::lint_improper_ctypes_only_phantomdata,
span_note: None, // filled later
});
ffiresult_recursor = None;
ffiresult_recursor = ControlFlow::Break(());
}
FfiResult::FfiUnsafe { ty, reason, help }
| FfiResult::FfiUnsafeWrapper { ty, reason, help, .. } => {
last_ty = Some(ty.clone());
let len = cimproper_layers.len();
if len > 0 {
cimproper_layers[len - 1].inner_ty = last_ty.clone();
if let Some(layer) = cimproper_layers.last_mut() {
layer.inner_ty = Some(ty.clone());
}
cimproper_layers.push(ImproperCTypesLayer {
ty: ty.clone(),
Expand All @@ -1632,22 +1627,18 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
});

if let FfiResult::FfiUnsafeWrapper { wrapped, .. } = ffir_rec {
ffiresult_recursor = Some(wrapped.as_ref());
ffiresult_recursor = ControlFlow::Continue(wrapped.as_ref());
} else {
ffiresult_recursor = None;
ffiresult_recursor = ControlFlow::Break(());
}
}
FfiResult::FfiSafe => {
bug!("malformed FfiResult stack: it should be unsafe all the way down")
}
};
}
let last_ty = match last_ty {
Some(last_ty) => last_ty,
None => bug!(
"This option should definitely have been filled by the loop that just finished"
),
};
// should always have at least one type
let last_ty = cimproper_layers.last().unwrap().ty.clone();
self.emit_ffi_unsafe_type_lint(last_ty, sp, cimproper_layers);
}
}
Expand Down

0 comments on commit 02072fd

Please sign in to comment.