Skip to content

(c2rust-analyze) Handle compatible ptr-to-ptr casts #840

Closed
@kkysen

Description

@kkysen

Currently, c2rust-analyze can't handle ptr-to-ptr casts when the pointee types are different, even when they're compatible/safely transmutable like u8 and i8/c_char. Thus, fixing this is necessary for c2rust transpiled string literal support (#833).

fn cast_only(s: *const u8) {
    s as *const core::ffi::c_char;
}
callgraph traversal order:
  DefId(0:3 ~ string_casts[c5c4]::cast_only)
visit_statement(StorageLive(_2))
visit_statement(StorageLive(_3))
visit_statement(_3 = _1)
rvalue = _1, desc = Project { base: PlaceRef { local: _1, projection: [] }, proj: [] }, base_lty = g0#*const u8[NONE#u8[]]
visit_rvalue(_1), desc = Some(Project { base: PlaceRef { local: _1, projection: [] }, proj: [] })
equate NONE#u8[] = NONE#u8[]
visit_statement(_2 = move _3 as *const i8 (Misc))
thread 'rustc' panicked at 'unexpected pointer type in *const i8', c2rust-analyze/src/context.rs:503:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/panicking.rs:142:14
   2: c2rust_analyze::context::label_no_pointers::{{closure}}
             at ./src/context.rs:503:9
   3: c2rust_analyze::labeled_ty::LabeledTyCtxt<L>::label
             at ./src/labeled_ty.rs:157:21
   4: c2rust_analyze::context::label_no_pointers
             at ./src/context.rs:502:5
   5: c2rust_analyze::context::AnalysisCtxt::type_of_rvalue
             at ./src/context.rs:368:17
   6: c2rust_analyze::dataflow::type_check::TypeChecker::visit_statement
             at ./src/dataflow/type_check.rs:233:30
   7: c2rust_analyze::dataflow::type_check::visit
             at ./src/dataflow/type_check.rs:388:13
   8: c2rust_analyze::dataflow::generate_constraints
             at ./src/dataflow/mod.rs:326:5
   9: c2rust_analyze::run
             at ./src/main.rs:480:45

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions