1
Fork 0

Require #[const_trait] for const impls

This commit is contained in:
Deadbeef 2022-07-30 18:01:03 +00:00
parent 22f6aec42d
commit 81b1810cd7
5 changed files with 27 additions and 0 deletions

View file

@ -192,6 +192,28 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
} }
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
let tcx = self.tcx;
if let hir::ItemKind::Impl(hir::Impl {
constness: hir::Constness::Const,
of_trait: Some(trait_ref),
..
}) = item.kind
{
let def_id = trait_ref.trait_def_id().unwrap();
let source_map = tcx.sess.source_map();
if !tcx.has_attr(def_id, sym::const_trait) {
tcx.sess
.struct_span_err(
source_map.guess_head_span(item.span),
"const `impl`s must be for traits marked with `#[const_trait]`",
)
.span_note(
source_map.guess_head_span(tcx.def_span(def_id)),
"this trait must be annotated with `#[const_trait]`",
)
.emit();
}
}
intravisit::walk_item(self, item); intravisit::walk_item(self, item);
} }

View file

@ -368,6 +368,7 @@ pub trait Into<T>: Sized {
all(_Self = "&str", T = "std::string::String"), all(_Self = "&str", T = "std::string::String"),
note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix", note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
))] ))]
#[const_trait]
pub trait From<T>: Sized { pub trait From<T>: Sized {
/// Converts to this type from the input type. /// Converts to this type from the input type.
#[lang = "from"] #[lang = "from"]

View file

@ -99,6 +99,7 @@
/// ``` /// ```
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")] #[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[const_trait]
pub trait Default: Sized { pub trait Default: Sized {
/// Returns the "default value" for a type. /// Returns the "default value" for a type.
/// ///

View file

@ -55,6 +55,7 @@
#[doc(alias = "]")] #[doc(alias = "]")]
#[doc(alias = "[")] #[doc(alias = "[")]
#[doc(alias = "[]")] #[doc(alias = "[]")]
#[const_trait]
pub trait Index<Idx: ?Sized> { pub trait Index<Idx: ?Sized> {
/// The returned type after indexing. /// The returned type after indexing.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
@ -163,6 +164,7 @@ see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#ind
#[doc(alias = "[")] #[doc(alias = "[")]
#[doc(alias = "]")] #[doc(alias = "]")]
#[doc(alias = "[]")] #[doc(alias = "[]")]
#[const_trait]
pub trait IndexMut<Idx: ?Sized>: Index<Idx> { pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
/// Performs the mutable indexing (`container[index]`) operation. /// Performs the mutable indexing (`container[index]`) operation.
/// ///

View file

@ -158,6 +158,7 @@ mod private_slice_index {
message = "the type `{T}` cannot be indexed by `{Self}`", message = "the type `{T}` cannot be indexed by `{Self}`",
label = "slice indices are of type `usize` or ranges of `usize`" label = "slice indices are of type `usize` or ranges of `usize`"
)] )]
#[const_trait]
pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed { pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
/// The output type returned by methods. /// The output type returned by methods.
#[stable(feature = "slice_get_slice", since = "1.28.0")] #[stable(feature = "slice_get_slice", since = "1.28.0")]