Skip to content

Commit 81b1810

Browse files
committed
Require #[const_trait] for const impls
1 parent 22f6aec commit 81b1810

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

compiler/rustc_passes/src/check_const.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
192192
}
193193

194194
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
195+
let tcx = self.tcx;
196+
if let hir::ItemKind::Impl(hir::Impl {
197+
constness: hir::Constness::Const,
198+
of_trait: Some(trait_ref),
199+
..
200+
}) = item.kind
201+
{
202+
let def_id = trait_ref.trait_def_id().unwrap();
203+
let source_map = tcx.sess.source_map();
204+
if !tcx.has_attr(def_id, sym::const_trait) {
205+
tcx.sess
206+
.struct_span_err(
207+
source_map.guess_head_span(item.span),
208+
"const `impl`s must be for traits marked with `#[const_trait]`",
209+
)
210+
.span_note(
211+
source_map.guess_head_span(tcx.def_span(def_id)),
212+
"this trait must be annotated with `#[const_trait]`",
213+
)
214+
.emit();
215+
}
216+
}
195217
intravisit::walk_item(self, item);
196218
}
197219

library/core/src/convert/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ pub trait Into<T>: Sized {
368368
all(_Self = "&str", T = "std::string::String"),
369369
note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
370370
))]
371+
#[const_trait]
371372
pub trait From<T>: Sized {
372373
/// Converts to this type from the input type.
373374
#[lang = "from"]

library/core/src/default.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
/// ```
100100
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
101101
#[stable(feature = "rust1", since = "1.0.0")]
102+
#[const_trait]
102103
pub trait Default: Sized {
103104
/// Returns the "default value" for a type.
104105
///

library/core/src/ops/index.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#[doc(alias = "]")]
5656
#[doc(alias = "[")]
5757
#[doc(alias = "[]")]
58+
#[const_trait]
5859
pub trait Index<Idx: ?Sized> {
5960
/// The returned type after indexing.
6061
#[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
163164
#[doc(alias = "[")]
164165
#[doc(alias = "]")]
165166
#[doc(alias = "[]")]
167+
#[const_trait]
166168
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
167169
/// Performs the mutable indexing (`container[index]`) operation.
168170
///

library/core/src/slice/index.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ mod private_slice_index {
158158
message = "the type `{T}` cannot be indexed by `{Self}`",
159159
label = "slice indices are of type `usize` or ranges of `usize`"
160160
)]
161+
#[const_trait]
161162
pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
162163
/// The output type returned by methods.
163164
#[stable(feature = "slice_get_slice", since = "1.28.0")]

0 commit comments

Comments
 (0)