rustc performs auto-ref when a raw pointer would be enough · Issue #73987 · rust-lang/rust (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

@RalfJung

Description

@RalfJung

The following code:

#![feature(slice_ptr_len)]

pub struct Test { data: [u8], }

pub fn test_len(t: *const Test) -> usize { unsafe { (*t).data.len() } }

generates MIR like

        _2 = &((*_1).0: [u8]);
        _0 = const core::slice::<impl [u8]>::len(move _2) -> bb1;

This means that a reference to data gets created, even though a raw pointer would be enough. That is a problem because creating a reference makes aliasing and validity assumptions that could be avoided. It would be better if rustc would not implicitly introduce such assumptions.

Cc @matthewjasper