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

Use Ancestory to check default fn in const impl instead of comparing idents #89471

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions compiler/rustc_passes/src/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! through, but errors for structured control flow in a `const` should be emitted here.

use rustc_attr as attr;
use rustc_data_structures::stable_set::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
Expand Down Expand Up @@ -83,30 +82,39 @@ impl<'tcx> hir::itemlikevisit::ItemLikeVisitor<'tcx> for CheckConstTraitVisitor<
let _: Option<_> = try {
if let hir::ItemKind::Impl(ref imp) = item.kind {
if let hir::Constness::Const = imp.constness {
let did = imp.of_trait.as_ref()?.trait_def_id()?;
let mut to_implement = FxHashSet::default();

for did in self.tcx.associated_item_def_ids(did) {
let trait_def_id = imp.of_trait.as_ref()?.trait_def_id()?;
let ancestors = self
.tcx
.trait_def(trait_def_id)
.ancestors(self.tcx, item.def_id.to_def_id())
.ok()?;
let mut to_implement = Vec::new();

for trait_item in self.tcx.associated_items(trait_def_id).in_definition_order()
{
if let ty::AssocItem {
kind: ty::AssocKind::Fn, ident, defaultness, ..
} = self.tcx.associated_item(*did)
} = trait_item
{
// we can ignore functions that do not have default bodies:
// if those are unimplemented it will be catched by typeck.
if defaultness.has_value()
&& !self.tcx.has_attr(*did, sym::default_method_body_is_const)
if !defaultness.has_value()
|| self
.tcx
.has_attr(trait_item.def_id, sym::default_method_body_is_const)
{
to_implement.insert(ident);
continue;
}
}
}

for it in imp
.items
.iter()
.filter(|it| matches!(it.kind, hir::AssocItemKind::Fn { .. }))
{
to_implement.remove(&it.ident);
let is_implemented = ancestors
.leaf_def(self.tcx, trait_item.ident, trait_item.kind)
.map(|node_item| !node_item.defining_node.is_from_trait())
.unwrap_or(false);

if !is_implemented {
to_implement.push(ident.to_string());
}
}
}

// all nonconst trait functions (not marked with #[default_method_body_is_const])
Expand All @@ -118,7 +126,7 @@ impl<'tcx> hir::itemlikevisit::ItemLikeVisitor<'tcx> for CheckConstTraitVisitor<
item.span,
"const trait implementations may not use non-const default functions",
)
.note(&format!("`{}` not implemented", to_implement.into_iter().map(|id| id.to_string()).collect::<Vec<_>>().join("`, `")))
.note(&format!("`{}` not implemented", to_implement.join("`, `")))
.emit();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(const_trait_impl)]
#![feature(const_fn_trait_bound)]

trait Tr {
fn req(&self);
Expand All @@ -18,11 +19,6 @@ impl const Tr for S {
fn req(&self) {}
} //~^^ ERROR const trait implementations may not use non-const default functions

impl const Tr for u8 {
fn req(&self) {}
fn prov(&self) {}
}

impl const Tr for u16 {
fn prov(&self) {}
fn default() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: const trait implementations may not use non-const default functions
--> $DIR/impl-with-default-fn.rs:17:1
--> $DIR/impl-with-default-fn-fail.rs:18:1
|
LL | / impl const Tr for S {
LL | | fn req(&self) {}
Expand All @@ -9,7 +9,7 @@ LL | | }
= note: `prov` not implemented

error: const trait implementations may not use non-const default functions
--> $DIR/impl-with-default-fn.rs:32:1
--> $DIR/impl-with-default-fn-fail.rs:28:1
|
LL | / impl const Tr for u32 {
LL | | fn req(&self) {}
Expand All @@ -20,7 +20,7 @@ LL | | }
= note: `prov` not implemented

error[E0046]: not all trait items implemented, missing: `req`
--> $DIR/impl-with-default-fn.rs:26:1
--> $DIR/impl-with-default-fn-fail.rs:22:1
|
LL | fn req(&self);
| -------------- `req` from trait
Expand Down
34 changes: 34 additions & 0 deletions src/test/ui/rfc-2632-const-trait-impl/impl-with-default-fn-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// check-pass

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

trait Tr {
fn req(&self);

fn prov(&self) {
println!("lul");
self.req();
}

#[default_method_body_is_const]
fn default() {}
}

impl const Tr for u8 {
fn req(&self) {}
fn prov(&self) {}
}

macro_rules! impl_tr {
($ty: ty) => {
impl const Tr for $ty {
fn req(&self) {}
fn prov(&self) {}
}
}
}

impl_tr!(u64);

fn main() {}