Require #[const_trait]
for const
impl
s
This commit is contained in:
parent
22f6aec42d
commit
81b1810cd7
5 changed files with 27 additions and 0 deletions
|
@ -192,6 +192,28 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'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);
|
||||
}
|
||||
|
||||
|
|
|
@ -368,6 +368,7 @@ pub trait Into<T>: Sized {
|
|||
all(_Self = "&str", T = "std::string::String"),
|
||||
note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
|
||||
))]
|
||||
#[const_trait]
|
||||
pub trait From<T>: Sized {
|
||||
/// Converts to this type from the input type.
|
||||
#[lang = "from"]
|
||||
|
|
|
@ -99,6 +99,7 @@
|
|||
/// ```
|
||||
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[const_trait]
|
||||
pub trait Default: Sized {
|
||||
/// Returns the "default value" for a type.
|
||||
///
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
#[doc(alias = "]")]
|
||||
#[doc(alias = "[")]
|
||||
#[doc(alias = "[]")]
|
||||
#[const_trait]
|
||||
pub trait Index<Idx: ?Sized> {
|
||||
/// The returned type after indexing.
|
||||
#[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 = "[]")]
|
||||
#[const_trait]
|
||||
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
|
||||
/// Performs the mutable indexing (`container[index]`) operation.
|
||||
///
|
||||
|
|
|
@ -158,6 +158,7 @@ mod private_slice_index {
|
|||
message = "the type `{T}` cannot be indexed by `{Self}`",
|
||||
label = "slice indices are of type `usize` or ranges of `usize`"
|
||||
)]
|
||||
#[const_trait]
|
||||
pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
|
||||
/// The output type returned by methods.
|
||||
#[stable(feature = "slice_get_slice", since = "1.28.0")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue