Skip to content

Commit 04171d0

Browse files
committed
Skip expected_projection if proj_ty is forbidden
1 parent 21e6de7 commit 04171d0

File tree

9 files changed

+92
-4
lines changed

9 files changed

+92
-4
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
609609
terr,
610610
false,
611611
false,
612+
false,
612613
);
613614
return Err(diag.emit());
614615
}
@@ -1031,6 +1032,7 @@ fn report_trait_method_mismatch<'tcx>(
10311032
terr,
10321033
false,
10331034
false,
1035+
false,
10341036
);
10351037

10361038
return diag.emit();
@@ -1838,6 +1840,7 @@ fn compare_const_predicate_entailment<'tcx>(
18381840
terr,
18391841
false,
18401842
false,
1843+
false,
18411844
);
18421845
return Err(diag.emit());
18431846
};

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ pub fn check_function_signature<'tcx>(
626626
err,
627627
false,
628628
false,
629+
false,
629630
);
630631
return Err(diag.emit());
631632
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10321032
e,
10331033
false,
10341034
true,
1035+
false,
10351036
);
10361037
}
10371038
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
15231523
terr: TypeError<'tcx>,
15241524
swap_secondary_and_primary: bool,
15251525
prefer_label: bool,
1526+
const_arg_has_assoc_type: bool,
15261527
) {
15271528
let span = cause.span();
15281529

@@ -1982,12 +1983,26 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19821983

19831984
self.check_and_note_conflicting_crates(diag, terr);
19841985

1985-
self.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());
1986+
self.note_and_explain_type_err(
1987+
diag,
1988+
terr,
1989+
cause,
1990+
span,
1991+
cause.body_id.to_def_id(),
1992+
const_arg_has_assoc_type,
1993+
);
19861994
if let Some(exp_found) = exp_found
19871995
&& let exp_found = TypeError::Sorts(exp_found)
19881996
&& exp_found != terr
19891997
{
1990-
self.note_and_explain_type_err(diag, exp_found, cause, span, cause.body_id.to_def_id());
1998+
self.note_and_explain_type_err(
1999+
diag,
2000+
exp_found,
2001+
cause,
2002+
span,
2003+
cause.body_id.to_def_id(),
2004+
false,
2005+
);
19912006
}
19922007

19932008
if let Some(ValuePairs::TraitRefs(exp_found)) = values
@@ -2169,7 +2184,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
21692184
self.type_error_additional_suggestions(&trace, terr),
21702185
);
21712186
let mut diag = self.tcx.dcx().create_err(failure_code);
2172-
self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr, false, false);
2187+
self.note_type_err(
2188+
&mut diag,
2189+
&trace.cause,
2190+
None,
2191+
Some(trace.values),
2192+
terr,
2193+
false,
2194+
false,
2195+
false,
2196+
);
21732197
diag
21742198
}
21752199

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2020
cause: &ObligationCause<'tcx>,
2121
sp: Span,
2222
body_owner_def_id: DefId,
23+
const_arg_has_assoc_type: bool,
2324
) {
2425
use ty::error::TypeError::*;
2526
debug!("note_and_explain_type_err err={:?} cause={:?}", err, cause);
@@ -242,7 +243,8 @@ impl<T> Trait<T> for X {
242243
}
243244
}
244245
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
245-
if !tcx.is_impl_trait_in_trait(proj_ty.def_id) =>
246+
if !tcx.is_impl_trait_in_trait(proj_ty.def_id)
247+
&& !const_arg_has_assoc_type =>
246248
{
247249
self.expected_projection(
248250
diag,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23972397
terr,
23982398
false,
23992399
false,
2400+
false,
24002401
);
24012402
diag.emit();
24022403
self.abort.set(true);

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
932932
TypeError::Sorts(ty::error::ExpectedFound::new(true, ty, ct.ty())),
933933
false,
934934
false,
935+
// proj_ty is forbidden as the type of a const generic parameter
936+
matches!(ty.kind(), ty::Alias(ty::Projection | ty::Inherent, _)),
935937
);
936938
diag
937939
}
@@ -1741,6 +1743,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
17411743
err,
17421744
true,
17431745
false,
1746+
false,
17441747
);
17451748
self.note_obligation_cause(&mut diag, obligation);
17461749
diag.emit()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(specialization)]
2+
#![allow(incomplete_features)]
3+
4+
trait Trait {
5+
type Type;
6+
}
7+
8+
impl Trait for i32 {
9+
default type Type = i32;
10+
}
11+
12+
struct Wrapper<const C: <i32 as Trait>::Type> {}
13+
//~^ ERROR `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter
14+
15+
impl<const C: usize> Wrapper<C> {}
16+
//~^ ERROR the constant `C` is not of type `<i32 as Trait>::Type`
17+
//~^^ ERROR mismatched types
18+
19+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter
2+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:12:25
3+
|
4+
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: the only supported types are integers, `bool` and `char`
8+
9+
error: the constant `C` is not of type `<i32 as Trait>::Type`
10+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:15:22
11+
|
12+
LL | impl<const C: usize> Wrapper<C> {}
13+
| ^^^^^^^^^^ expected associated type, found `usize`
14+
|
15+
note: required by a bound in `Wrapper`
16+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:12:16
17+
|
18+
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Wrapper`
20+
21+
error[E0308]: mismatched types
22+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:15:30
23+
|
24+
LL | impl<const C: usize> Wrapper<C> {}
25+
| ^ expected associated type, found `usize`
26+
|
27+
= note: expected associated type `<i32 as Trait>::Type`
28+
found type `usize`
29+
= help: consider constraining the associated type `<i32 as Trait>::Type` to `usize`
30+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
31+
32+
error: aborting due to 3 previous errors
33+
34+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)