Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const fn can have little a phantom data #67649

Open
Manishearth opened this issue Dec 27, 2019 · 4 comments
Open

const fn can have little a phantom data #67649

Manishearth opened this issue Dec 27, 2019 · 4 comments

Comments

@Manishearth
Copy link
Member

@Manishearth Manishearth commented Dec 27, 2019

use std::marker::PhantomData;

fn main() {}

const fn foo() -> PhantomData<fn()> {
   PhantomData
}

Playground

   Compiling playground v0.0.1 (/playground)
error[E0723]: function pointers in const fn are unstable
 --> src/main.rs:7:19
  |
7 | const fn foo() -> PhantomData<fn()> {
  |                   ^^^^^^^^^^^^^^^^^
  |
  = note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
  = help: add `#![feature(const_fn)]` to the crate attributes to enable

We should probably not restrict the phantom datas a const fn is allowed to construct

cc @oli-obk

@oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Dec 27, 2019

@Centril
Copy link
Contributor

@Centril Centril commented Dec 27, 2019

We should probably not restrict the phantom datas a const fn is allowed to construct

I don't see anything special about PhantomData's type parameter as opposed to other ones for other types in terms of what the meaning of fn() is and whether it would conditionally mean e.g. const fn(). At any rate, I don't think we should be adding special allowances for this type before we holistically consider everything else.

@oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Dec 27, 2019

All the type based limitations of const fn are things that we plan to resolve. Right now we have a very simple check

fn check_ty(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, fn_def_id: DefId) -> McfResult {
for ty in ty.walk() {
match ty.kind {
ty::Ref(_, _, hir::Mutability::Mut) => {
if !feature_allowed(tcx, fn_def_id, sym::const_mut_refs) {
return Err((span, "mutable references in const fn are unstable".into()));
}
}
ty::Opaque(..) => return Err((span, "`impl Trait` in const fn is unstable".into())),
ty::FnPtr(..) => {
if !tcx.const_fn_is_allowed_fn_ptr(fn_def_id) {
return Err((span, "function pointers in const fn are unstable".into()));
}
}
ty::Dynamic(preds, _) => {
for pred in preds.iter() {
match pred.skip_binder() {
ty::ExistentialPredicate::AutoTrait(_)
| ty::ExistentialPredicate::Projection(_) => {
return Err((
span,
"trait bounds other than `Sized` \
on const fn parameters are unstable"
.into(),
));
}
ty::ExistentialPredicate::Trait(trait_ref) => {
if Some(trait_ref.def_id) != tcx.lang_items().sized_trait() {
return Err((
span,
"trait bounds other than `Sized` \
on const fn parameters are unstable"
.into(),
));
}
}
}
}
}
_ => {}
}
}
Ok(())
}

adding a PhantomData exception will make this check more complex. A real world example that makes this feature useful would be good as motiviation to do this change.

@Manishearth
Copy link
Member Author

@Manishearth Manishearth commented Dec 27, 2019

@Centril right, I'm talking about types which transitively use phantom data as well. I believe there are other similar exceptions in the compiler for PhantomData.

As for a real world use case, @hawkw hit this but I don't recall the specifics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.