Remove #[default..]
and add #[const_trait]
This commit is contained in:
parent
f558990814
commit
257f06587c
30 changed files with 74 additions and 250 deletions
|
@ -13,7 +13,6 @@ use rustc_hir as hir;
|
|||
use rustc_hir::def_id::LocalDefId;
|
||||
use rustc_hir::intravisit::{self, Visitor};
|
||||
use rustc_middle::hir::nested_filter;
|
||||
use rustc_middle::ty;
|
||||
use rustc_middle::ty::query::Providers;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_session::parse::feature_err;
|
||||
|
@ -64,66 +63,6 @@ pub(crate) fn provide(providers: &mut Providers) {
|
|||
*providers = Providers { check_mod_const_bodies, ..*providers };
|
||||
}
|
||||
|
||||
fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
|
||||
let _: Option<_> = try {
|
||||
if let hir::ItemKind::Impl(ref imp) = item.kind && let hir::Constness::Const = imp.constness {
|
||||
let trait_def_id = imp.of_trait.as_ref()?.trait_def_id()?;
|
||||
let ancestors = tcx
|
||||
.trait_def(trait_def_id)
|
||||
.ancestors(tcx, item.def_id.to_def_id())
|
||||
.ok()?;
|
||||
let mut to_implement = Vec::new();
|
||||
|
||||
for trait_item in tcx.associated_items(trait_def_id).in_definition_order()
|
||||
{
|
||||
if let ty::AssocItem {
|
||||
kind: ty::AssocKind::Fn,
|
||||
defaultness,
|
||||
def_id: trait_item_id,
|
||||
..
|
||||
} = *trait_item
|
||||
{
|
||||
// we can ignore functions that do not have default bodies:
|
||||
// if those are unimplemented it will be caught by typeck.
|
||||
if !defaultness.has_value()
|
||||
|| tcx
|
||||
.has_attr(trait_item_id, sym::default_method_body_is_const)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let is_implemented = ancestors
|
||||
.leaf_def(tcx, trait_item_id)
|
||||
.map(|node_item| !node_item.defining_node.is_from_trait())
|
||||
.unwrap_or(false);
|
||||
|
||||
if !is_implemented {
|
||||
to_implement.push(trait_item_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// all nonconst trait functions (not marked with #[default_method_body_is_const])
|
||||
// must be implemented
|
||||
if !to_implement.is_empty() {
|
||||
let not_implemented = to_implement
|
||||
.into_iter()
|
||||
.map(|did| tcx.item_name(did).to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("`, `");
|
||||
tcx
|
||||
.sess
|
||||
.struct_span_err(
|
||||
item.span,
|
||||
"const trait implementations may not use non-const default functions",
|
||||
)
|
||||
.note(&format!("`{}` not implemented", not_implemented))
|
||||
.emit();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct CheckConstVisitor<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
|
@ -254,7 +193,6 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
|
|||
|
||||
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
||||
intravisit::walk_item(self, item);
|
||||
check_item(self.tcx, item);
|
||||
}
|
||||
|
||||
fn visit_anon_const(&mut self, anon: &'tcx hir::AnonConst) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue