Skip to content

Commit 139f63a

Browse files
committed
Auto merge of rust-lang#116015 - EvanMerlock:master, r=oli-obk
const_eval: allow function pointer signatures containing &mut T in const contexts potentially fixes rust-lang#114994 We utilize a `TypeVisitor` here in order to more easily handle control flow. - In the event the typekind the Visitor sees is a function pointer, we skip over it - However, otherwise we do one of two things: - If we find a mutable reference, check it, then continue visiting types - If we find any other type, continue visiting types This means we will check if the function pointer _itself_ is mutable, but not if any of the types _within_ are.
2 parents 481d45a + d975ae5 commit 139f63a

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
99
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
1010
use rustc_middle::mir::*;
1111
use rustc_middle::traits::BuiltinImplSource;
12+
use rustc_middle::ty::GenericArgs;
1213
use rustc_middle::ty::{self, adjustment::PointerCoercion, Instance, InstanceDef, Ty, TyCtxt};
13-
use rustc_middle::ty::{GenericArgKind, GenericArgs};
1414
use rustc_middle::ty::{TraitRef, TypeVisitableExt};
1515
use rustc_mir_dataflow::{self, Analysis};
1616
use rustc_span::{sym, Span, Symbol};
1717
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
1818
use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt, SelectionContext};
19+
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitor};
1920

2021
use std::mem;
21-
use std::ops::Deref;
22+
use std::ops::{ControlFlow, Deref};
2223

2324
use super::ops::{self, NonConstOp, Status};
2425
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop};
@@ -188,6 +189,24 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
188189
}
189190
}
190191

192+
struct LocalReturnTyVisitor<'ck, 'mir, 'tcx> {
193+
kind: LocalKind,
194+
checker: &'ck mut Checker<'mir, 'tcx>,
195+
}
196+
197+
impl<'ck, 'mir, 'tcx> TypeVisitor<TyCtxt<'tcx>> for LocalReturnTyVisitor<'ck, 'mir, 'tcx> {
198+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
199+
match t.kind() {
200+
ty::FnPtr(_) => ControlFlow::Continue(()),
201+
ty::Ref(_, _, hir::Mutability::Mut) => {
202+
self.checker.check_op(ops::ty::MutRef(self.kind));
203+
t.super_visit_with(self)
204+
}
205+
_ => t.super_visit_with(self),
206+
}
207+
}
208+
}
209+
191210
pub struct Checker<'mir, 'tcx> {
192211
ccx: &'mir ConstCx<'mir, 'tcx>,
193212
qualifs: Qualifs<'mir, 'tcx>,
@@ -346,20 +365,9 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
346365
fn check_local_or_return_ty(&mut self, ty: Ty<'tcx>, local: Local) {
347366
let kind = self.body.local_kind(local);
348367

349-
for ty in ty.walk() {
350-
let ty = match ty.unpack() {
351-
GenericArgKind::Type(ty) => ty,
352-
353-
// No constraints on lifetimes or constants, except potentially
354-
// constants' types, but `walk` will get to them as well.
355-
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => continue,
356-
};
368+
let mut visitor = LocalReturnTyVisitor { kind, checker: self };
357369

358-
match *ty.kind() {
359-
ty::Ref(_, _, hir::Mutability::Mut) => self.check_op(ops::ty::MutRef(kind)),
360-
_ => {}
361-
}
362-
}
370+
visitor.visit_ty(ty);
363371
}
364372

365373
fn check_mut_borrow(&mut self, local: Local, kind: hir::BorrowKind) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This checks that function pointer signatures that are referenced mutably
2+
// but contain a &mut T parameter still fail in a constant context: see issue #114994.
3+
//
4+
// check-fail
5+
6+
const fn use_mut_const_fn(_f: &mut fn(&mut String)) { //~ ERROR mutable references are not allowed in constant functions
7+
()
8+
}
9+
10+
const fn use_mut_const_tuple_fn(_f: (fn(), &mut u32)) { //~ ERROR mutable references are not allowed in constant functions
11+
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0658]: mutable references are not allowed in constant functions
2+
--> $DIR/issue-114994-fail.rs:6:27
3+
|
4+
LL | const fn use_mut_const_fn(_f: &mut fn(&mut String)) {
5+
| ^^
6+
|
7+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
9+
10+
error[E0658]: mutable references are not allowed in constant functions
11+
--> $DIR/issue-114994-fail.rs:10:33
12+
|
13+
LL | const fn use_mut_const_tuple_fn(_f: (fn(), &mut u32)) {
14+
| ^^
15+
|
16+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
17+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This checks that function pointer signatures containing &mut T types
2+
// work in a constant context: see issue #114994.
3+
//
4+
// check-pass
5+
6+
const fn use_const_fn(_f: fn(&mut String)) {
7+
()
8+
}
9+
10+
const fn get_some_fn() -> fn(&mut String) {
11+
String::clear
12+
}
13+
14+
const fn some_const_fn() {
15+
let _f: fn(&mut String) = String::clear;
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)