Load missing type of impl associated constant from trait definition

This commit is contained in:
Oli Scherer 2024-03-27 10:56:11 +00:00
parent b13a71a2e7
commit 804c047657
6 changed files with 89 additions and 17 deletions

View file

@ -192,10 +192,20 @@ fn typeck_with_fallback<'tcx>(
check_fn(&mut fcx, fn_sig, None, decl, def_id, body, tcx.features().unsized_fn_params);
} else {
let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = body_ty {
Some(fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
}))
if let Some(item) = tcx.opt_associated_item(def_id.into())
&& let ty::AssocKind::Const = item.kind
&& let ty::ImplContainer = item.container
&& let Some(trait_item) = item.trait_item_def_id
{
let args =
tcx.impl_trait_ref(item.container_id(tcx)).unwrap().instantiate_identity().args;
Some(tcx.type_of(trait_item).instantiate(tcx, args))
} else {
Some(fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
}))
}
} else if let Node::AnonConst(_) = node {
match tcx.parent_hir_node(id) {
Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), .. })

View file

@ -0,0 +1,28 @@
//! Test that we compute the right type for associated constants
//! of impls, even if the type is missing. We know it from the trait
//! declaration after all.
trait Range {
const FIRST: u8;
const LAST: u8;
}
struct TwoDigits;
impl Range for TwoDigits {
const FIRST: = 10;
//~^ ERROR: missing type for `const` item
const LAST: u8 = 99;
}
const fn digits(x: u8) -> usize {
match x {
TwoDigits::FIRST..=TwoDigits::LAST => 0,
0..=9 | 100..=255 => panic!(),
}
}
const FOOMP: [(); {
digits(42)
}] = [];
fn main() {}

View file

@ -0,0 +1,8 @@
error: missing type for `const` item
--> $DIR/missing_assoc_const_type.rs:12:17
|
LL | const FIRST: = 10;
| ^ help: provide a type for the associated constant: `u8`
error: aborting due to 1 previous error

View file

@ -0,0 +1,21 @@
//! Test that we compute the right type for associated constants
//! of impls, even if the type is missing. We know it from the trait
//! declaration after all.
trait Range {
const FIRST: u8;
const LAST: u8;
}
struct TwoDigits;
impl Range for TwoDigits {
const FIRST: = 10;
//~^ ERROR: missing type
const LAST: u8 = 99;
}
const FOOMP: [(); {
TwoDigits::FIRST as usize
}] = [(); 10];
fn main() {}

View file

@ -0,0 +1,8 @@
error: missing type for `const` item
--> $DIR/missing_assoc_const_type2.rs:12:17
|
LL | const FIRST: = 10;
| ^ help: provide a type for the associated constant: `u8`
error: aborting due to 1 previous error

View file

@ -546,6 +546,15 @@ help: use type parameters instead
LL | fn fn_test10<T>(&self, _x : T) { }
| +++ ~
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item.rs:194:14
|
LL | const D: _ = 42;
| ^
| |
| not allowed in type signatures
| help: replace with the correct type: `i32`
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
--> $DIR/typeck_type_placeholder_item.rs:217:31
|
@ -574,19 +583,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
--> $DIR/typeck_type_placeholder_item.rs:209:14
|
LL | const D: _ = 42;
| ^
| |
| not allowed in type signatures
| help: replace with the correct type: `i32`
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item.rs:194:14
|
LL | const D: _ = 42;
| ^
| |
| not allowed in type signatures
| help: replace with the correct type: `i32`
| ^ not allowed in type signatures
error[E0046]: not all trait items implemented, missing: `F`
--> $DIR/typeck_type_placeholder_item.rs:200:1