Skip to content

Commit

Permalink
more targeted errors when extern types end up in places they should not
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Dec 2, 2023
1 parent 0908f17 commit b5ecb9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {

#[instrument(level = "trace", skip(self))]
fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
if place.layout.is_unsized() {
let tail = self.tcx.struct_tail_with_normalize(place.layout.ty, |ty| ty, || {});
if matches!(tail.kind(), ty::Foreign(..)) {
// Unsized locals and, at least conceptually, even unsized arguments must be copied
// around, which requires dynamically determining their size. Therefore, we cannot
// allow `extern` types here. Consult t-opsem before removing this check.
panic!("unsized locals must not be `extern` types");
}
}
assert_eq!(place.llextra.is_some(), place.layout.is_unsized());

if place.layout.is_zst() {
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ fn fn_abi_sanity_check<'tcx>(
PassMode::Indirect { meta_attrs: Some(_), on_stack, .. } => {
// With metadata. Must be unsized and not on the stack.
assert!(arg.layout.is_unsized() && !on_stack);
// Also, must not be `extern` type.
let tail = cx.tcx.struct_tail_with_normalize(arg.layout.ty, |ty| ty, || {});
if matches!(tail.kind(), ty::Foreign(..)) {
// These types do not have metadata, so having `meta_attrs` is bogus.
// Conceptually, unsized arguments must be copied around, which requires dynamically
// determining their size. Therefore, we cannot allow `extern` types here. Consult
// t-opsem before removing this check.
panic!("unsized arguments must not be `extern` types");
}
}
}
}
Expand Down

0 comments on commit b5ecb9d

Please sign in to comment.