Skip to content

Commit b2e304e

Browse files
fmeasefee1-dead
andcommitted
Prevent ICE on ~const trait bounds involving non-const traits
Co-authored-by: Deadbeef <[email protected]>
1 parent bf9229a commit b2e304e

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

compiler/rustc_hir_analysis/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub fn create_args_for_parent_generic_args<'tcx, 'a>(
262262
// impl const PartialEq for () {}
263263
// ```
264264
//
265-
// Since this is a const impl, we need to insert `<false>` at the end of
265+
// Since this is a const impl, we need to insert a host arg at the end of
266266
// `PartialEq`'s generics, but this errors since `Rhs` isn't specified.
267267
// To work around this, we infer all arguments until we reach the host param.
268268
args.push(ctx.inferred_kind(Some(&args), param, infer_args));

compiler/rustc_hir_analysis/src/astconv/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
378378
assert!(self_ty.is_none());
379379
}
380380

381-
let arg_count = check_generic_arg_count(
381+
let mut arg_count = check_generic_arg_count(
382382
tcx,
383383
span,
384384
def_id,
@@ -390,6 +390,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
390390
infer_args,
391391
);
392392

393+
if let ty::BoundConstness::ConstIfConst = constness
394+
&& generics.has_self
395+
&& !tcx.has_attr(def_id, sym::const_trait)
396+
{
397+
let reported = tcx.sess.emit_err(crate::errors::ConstBoundForNonConstTrait { span });
398+
arg_count.correct =
399+
Err(GenericArgCountMismatch { reported: Some(reported), invalid_args: vec![] });
400+
}
401+
393402
// Skip processing if type has no generic parameters.
394403
// Traits always have `Self` as a generic parameter, which means they will not return early
395404
// here and so associated type bindings will be handled regardless of whether there are any
@@ -570,13 +579,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
570579
&mut args_ctx,
571580
);
572581

573-
if let ty::BoundConstness::ConstIfConst = constness
574-
&& generics.has_self
575-
&& !tcx.has_attr(def_id, sym::const_trait)
576-
{
577-
tcx.sess.emit_err(crate::errors::ConstBoundForNonConstTrait { span });
578-
}
579-
580582
(args, arg_count)
581583
}
582584

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test for issue #117244.
2+
#![feature(const_trait_impl, effects)]
3+
4+
trait NonConst {}
5+
6+
const fn perform<T: ~const NonConst>() {}
7+
//~^ ERROR ~const can only be applied to `#[const_trait]` traits
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: ~const can only be applied to `#[const_trait]` traits
2+
--> $DIR/tilde-const-on-non-const-trait.rs:6:28
3+
|
4+
LL | const fn perform<T: ~const NonConst>() {}
5+
| ^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)