Skip to content

Commit

Permalink
Prevent ICE on ~const trait bounds involving non-const traits
Browse files Browse the repository at this point in the history
Co-authored-by: Deadbeef <ent3rm4n@gmail.com>
  • Loading branch information
fmease and fee1-dead committed Dec 19, 2023
1 parent bf9229a commit b2e304e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub fn create_args_for_parent_generic_args<'tcx, 'a>(
// impl const PartialEq for () {}
// ```
//
// Since this is a const impl, we need to insert `<false>` at the end of
// Since this is a const impl, we need to insert a host arg at the end of
// `PartialEq`'s generics, but this errors since `Rhs` isn't specified.
// To work around this, we infer all arguments until we reach the host param.
args.push(ctx.inferred_kind(Some(&args), param, infer_args));
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
assert!(self_ty.is_none());
}

let arg_count = check_generic_arg_count(
let mut arg_count = check_generic_arg_count(
tcx,
span,
def_id,
Expand All @@ -390,6 +390,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
infer_args,
);

if let ty::BoundConstness::ConstIfConst = constness
&& generics.has_self
&& !tcx.has_attr(def_id, sym::const_trait)
{
let reported = tcx.sess.emit_err(crate::errors::ConstBoundForNonConstTrait { span });
arg_count.correct =
Err(GenericArgCountMismatch { reported: Some(reported), invalid_args: vec![] });
}

// Skip processing if type has no generic parameters.
// Traits always have `Self` as a generic parameter, which means they will not return early
// here and so associated type bindings will be handled regardless of whether there are any
Expand Down Expand Up @@ -570,13 +579,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
&mut args_ctx,
);

if let ty::BoundConstness::ConstIfConst = constness
&& generics.has_self
&& !tcx.has_attr(def_id, sym::const_trait)
{
tcx.sess.emit_err(crate::errors::ConstBoundForNonConstTrait { span });
}

(args, arg_count)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Regression test for issue #117244.
#![feature(const_trait_impl, effects)]

trait NonConst {}

const fn perform<T: ~const NonConst>() {}
//~^ ERROR ~const can only be applied to `#[const_trait]` traits

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/tilde-const-on-non-const-trait.rs:6:28
|
LL | const fn perform<T: ~const NonConst>() {}
| ^^^^^^^^

error: aborting due to 1 previous error

0 comments on commit b2e304e

Please sign in to comment.