Skip to content

Commit f1cd746

Browse files
committed
Fix const Add
1 parent bd1c55a commit f1cd746

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

compiler/rustc_ast_lowering/src/item.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
188188
let (generics, (ty, body_id)) = self.lower_generics(
189189
generics,
190190
Const::No,
191+
false,
191192
id,
192193
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
193194
|this| {
@@ -218,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
218219

219220
let itctx = ImplTraitContext::Universal;
220221
let (generics, decl) =
221-
this.lower_generics(generics, header.constness, id, itctx, |this| {
222+
this.lower_generics(generics, header.constness, false, id, itctx, |this| {
222223
this.lower_fn_decl(
223224
decl,
224225
id,
@@ -262,6 +263,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
262263
let (generics, ty) = self.lower_generics(
263264
&generics,
264265
Const::No,
266+
false,
265267
id,
266268
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
267269
|this| match ty {
@@ -284,6 +286,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
284286
let (generics, variants) = self.lower_generics(
285287
generics,
286288
Const::No,
289+
false,
287290
id,
288291
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
289292
|this| {
@@ -298,6 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
298301
let (generics, struct_def) = self.lower_generics(
299302
generics,
300303
Const::No,
304+
false,
301305
id,
302306
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
303307
|this| this.lower_variant_data(hir_id, struct_def),
@@ -308,6 +312,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
308312
let (generics, vdata) = self.lower_generics(
309313
generics,
310314
Const::No,
315+
false,
311316
id,
312317
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
313318
|this| this.lower_variant_data(hir_id, vdata),
@@ -339,7 +344,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
339344
// parent lifetime.
340345
let itctx = ImplTraitContext::Universal;
341346
let (generics, (trait_ref, lowered_ty)) =
342-
self.lower_generics(ast_generics, Const::No, id, itctx, |this| {
347+
self.lower_generics(ast_generics, Const::No, false, id, itctx, |this| {
343348
let modifiers = TraitBoundModifiers {
344349
constness: BoundConstness::Never,
345350
asyncness: BoundAsyncness::Normal,
@@ -391,6 +396,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
391396
let (generics, (unsafety, items, bounds)) = self.lower_generics(
392397
generics,
393398
Const::No,
399+
false,
394400
id,
395401
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
396402
|this| {
@@ -411,6 +417,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
411417
let (generics, bounds) = self.lower_generics(
412418
generics,
413419
Const::No,
420+
false,
414421
id,
415422
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
416423
|this| {
@@ -645,7 +652,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
645652
let fdec = &sig.decl;
646653
let itctx = ImplTraitContext::Universal;
647654
let (generics, (fn_dec, fn_args)) =
648-
self.lower_generics(generics, Const::No, i.id, itctx, |this| {
655+
self.lower_generics(generics, Const::No, false, i.id, itctx, |this| {
649656
(
650657
// Disallow `impl Trait` in foreign items.
651658
this.lower_fn_decl(
@@ -773,6 +780,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
773780
let (generics, kind) = self.lower_generics(
774781
generics,
775782
Const::No,
783+
false,
776784
i.id,
777785
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
778786
|this| {
@@ -821,6 +829,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
821829
let (generics, kind) = self.lower_generics(
822830
&generics,
823831
Const::No,
832+
false,
824833
i.id,
825834
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
826835
|this| {
@@ -892,7 +901,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
892901
fn lower_impl_item(
893902
&mut self,
894903
i: &AssocItem,
895-
impl_constness: Const,
904+
constness_of_trait: Const,
896905
) -> &'hir hir::ImplItem<'hir> {
897906
// Since `default impl` is not yet implemented, this is always true in impls.
898907
let has_value = true;
@@ -904,6 +913,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
904913
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => self.lower_generics(
905914
generics,
906915
Const::No,
916+
false,
907917
i.id,
908918
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
909919
|this| {
@@ -928,7 +938,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
928938
i.id,
929939
if self.is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
930940
sig.header.coroutine_kind,
931-
impl_constness,
941+
constness_of_trait,
932942
);
933943

934944
(generics, hir::ImplItemKind::Fn(sig, body_id))
@@ -939,6 +949,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
939949
self.lower_generics(
940950
&generics,
941951
Const::No,
952+
false,
942953
i.id,
943954
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
944955
|this| match ty {
@@ -1349,9 +1360,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13491360
let constness =
13501361
if kind == FnDeclKind::Inherent { sig.header.constness } else { parent_constness };
13511362
let itctx = ImplTraitContext::Universal;
1352-
let (generics, decl) = self.lower_generics(generics, constness, id, itctx, |this| {
1353-
this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)
1354-
});
1363+
let (generics, decl) =
1364+
self.lower_generics(generics, constness, kind == FnDeclKind::Impl, id, itctx, |this| {
1365+
this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)
1366+
});
13551367
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
13561368
}
13571369

@@ -1426,6 +1438,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14261438
&mut self,
14271439
generics: &Generics,
14281440
constness: Const,
1441+
force_append_constness: bool,
14291442
parent_node_id: NodeId,
14301443
itctx: ImplTraitContext,
14311444
f: impl FnOnce(&mut Self) -> T,
@@ -1486,7 +1499,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14861499
// if the effects feature is enabled. This needs to be done before we lower where
14871500
// clauses since where clauses need to bind to the DefId of the host param
14881501
let host_param_parts = if let Const::Yes(span) = constness
1489-
&& self.tcx.features().effects
1502+
// if this comes from implementing a `const` trait, we must force constness to be appended
1503+
// to the impl item, no matter whether effects is enabled.
1504+
&& (self.tcx.features().effects || force_append_constness)
14901505
{
14911506
let span = self.lower_span(span);
14921507
let param_node_id = self.next_node_id();

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
4242
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => {
4343
hir::Constness::Const
4444
}
45-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. }) => tcx
46-
.associated_type_for_effects(def_id)
47-
.map_or(hir::Constness::NotConst, |_| hir::Constness::Const),
45+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
4846
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
4947
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
5048
// foreign items cannot be evaluated at compile-time.

compiler/rustc_hir_analysis/src/astconv/object_safety.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
169169
tcx.associated_items(pred.def_id())
170170
.in_definition_order()
171171
.filter(|item| item.kind == ty::AssocKind::Type)
172-
.filter(|item| !item.is_impl_trait_in_trait())
172+
.filter(|item| {
173+
!item.is_impl_trait_in_trait() && !item.is_effects_desugaring
174+
})
173175
.map(|item| item.def_id),
174176
);
175177
}

compiler/rustc_hir_typeck/src/method/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::traits::ObligationCause;
2121
use rustc_middle::ty::{self, GenericParamDefKind, Ty, TypeVisitableExt};
2222
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
2323
use rustc_span::symbol::Ident;
24-
use rustc_span::Span;
24+
use rustc_span::{sym, Span};
2525
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2626
use rustc_trait_selection::traits::{self, NormalizeExt};
2727

@@ -358,6 +358,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
358358
) -> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
359359
let (obligation, args) =
360360
self.obligation_for_method(cause, trait_def_id, self_ty, opt_input_types);
361+
// FIXME(effects) find a better way to do this
362+
// Operators don't have generic methods, but making them `#[const_trait]` gives them
363+
// `const host: bool`.
364+
let args = if self.tcx.has_attr(trait_def_id, sym::const_trait) {
365+
self.tcx.mk_args_from_iter(
366+
args.iter()
367+
.chain([self.tcx.expected_host_effect_param_for_body(self.body_id).into()]),
368+
)
369+
} else {
370+
args
371+
};
361372
self.construct_obligation_for_trait(m_name, trait_def_id, obligation, args)
362373
}
363374

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
#![feature(const_maybe_uninit_uninit_array)]
146146
#![feature(const_nonnull_new)]
147147
#![feature(const_num_midpoint)]
148+
#![feature(const_ops)]
148149
#![feature(const_option)]
149150
#![feature(const_option_ext)]
150151
#![feature(const_pin)]

library/core/src/ops/arith.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
append_const_msg
7474
)]
7575
#[doc(alias = "+")]
76-
// FIXME(effects) #[const_trait]
76+
#[const_trait]
7777
pub trait Add<Rhs = Self> {
7878
/// The resulting type after applying the `+` operator.
7979
#[stable(feature = "rust1", since = "1.0.0")]
@@ -95,8 +95,8 @@ pub trait Add<Rhs = Self> {
9595
macro_rules! add_impl {
9696
($($t:ty)*) => ($(
9797
#[stable(feature = "rust1", since = "1.0.0")]
98-
// FIXME(effects) #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
99-
impl /* const */ Add for $t {
98+
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
99+
impl const Add for $t {
100100
type Output = $t;
101101

102102
#[inline]

0 commit comments

Comments
 (0)