Skip to content

Commit

Permalink
Rollup merge of rust-lang#136314 - compiler-errors:const-deref-adj, r…
Browse files Browse the repository at this point in the history
…=fee1-dead

Use proper type when applying deref adjustment in const

When applying a deref adjustment to some type `Wrap<T>` which derefs to `T`, we were checking that `T: ~const Deref`, not `Wrap<T>: ~const Deref` like we should have been.

r? project-const-traits

Fixes rust-lang#136273
Fixes rust-lang#135210 -- I just deleted the test since the regression test is uninteresting
  • Loading branch information
matthiaskrgr authored Feb 1, 2025
2 parents 4b310be + c64038a commit 47e6684
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}

let mut expr_ty = self.typeck_results.borrow().expr_ty_adjusted(expr);

for a in &adj {
match a.kind {
Adjust::NeverToAny => {
Expand All @@ -266,7 +268,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
None,
expr.span,
overloaded_deref.method_call(self.tcx),
self.tcx.mk_args(&[a.target.into()]),
self.tcx.mk_args(&[expr_ty.into()]),
);
}
Adjust::Deref(None) => {
Expand All @@ -283,6 +285,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// No effects to enforce here.
}
}

expr_ty = a.target;
}

let autoborrow_mut = adj.iter().any(|adj| {
Expand Down
8 changes: 0 additions & 8 deletions tests/crashes/135210.rs

This file was deleted.

28 changes: 28 additions & 0 deletions tests/ui/traits/const-traits/enforce-deref-on-adjust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@ check-pass

#![feature(const_deref)]
#![feature(const_trait_impl)]

use std::ops::Deref;

struct Wrap<T>(T);
struct Foo;

impl Foo {
const fn call(&self) {}
}

impl<T> const Deref for Wrap<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

const fn foo() {
let x = Wrap(Foo);
x.call();
}

fn main() {}

0 comments on commit 47e6684

Please sign in to comment.