diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2024-03-27 10:56:11 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2024-03-27 11:02:53 +0000 |
| commit | 804c0476575e891aa47131d67cbb3b6c21682abf (patch) | |
| tree | 216c200cdabae706eeab6cff996fb04eb34f92ee | |
| parent | b13a71a2e77f4625d1a2b8a5b9488414686ebca9 (diff) | |
| download | rust-804c0476575e891aa47131d67cbb3b6c21682abf.tar.gz rust-804c0476575e891aa47131d67cbb3b6c21682abf.zip | |
Load missing type of impl associated constant from trait definition
| -rw-r--r-- | compiler/rustc_hir_typeck/src/lib.rs | 18 | ||||
| -rw-r--r-- | tests/ui/consts/missing_assoc_const_type.rs | 28 | ||||
| -rw-r--r-- | tests/ui/consts/missing_assoc_const_type.stderr | 8 | ||||
| -rw-r--r-- | tests/ui/consts/missing_assoc_const_type2.rs | 21 | ||||
| -rw-r--r-- | tests/ui/consts/missing_assoc_const_type2.stderr | 8 | ||||
| -rw-r--r-- | tests/ui/typeck/typeck_type_placeholder_item.stderr | 23 |
6 files changed, 89 insertions, 17 deletions
diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 0b67b37df29..f201bfddf4b 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -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), .. }) diff --git a/tests/ui/consts/missing_assoc_const_type.rs b/tests/ui/consts/missing_assoc_const_type.rs new file mode 100644 index 00000000000..8d95e3dca63 --- /dev/null +++ b/tests/ui/consts/missing_assoc_const_type.rs @@ -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() {} diff --git a/tests/ui/consts/missing_assoc_const_type.stderr b/tests/ui/consts/missing_assoc_const_type.stderr new file mode 100644 index 00000000000..28af1f0f321 --- /dev/null +++ b/tests/ui/consts/missing_assoc_const_type.stderr @@ -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 + diff --git a/tests/ui/consts/missing_assoc_const_type2.rs b/tests/ui/consts/missing_assoc_const_type2.rs new file mode 100644 index 00000000000..baf236700a3 --- /dev/null +++ b/tests/ui/consts/missing_assoc_const_type2.rs @@ -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() {} diff --git a/tests/ui/consts/missing_assoc_const_type2.stderr b/tests/ui/consts/missing_assoc_const_type2.stderr new file mode 100644 index 00000000000..1255ca2d102 --- /dev/null +++ b/tests/ui/consts/missing_assoc_const_type2.stderr @@ -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 + diff --git a/tests/ui/typeck/typeck_type_placeholder_item.stderr b/tests/ui/typeck/typeck_type_placeholder_item.stderr index 8bcad56916a..7977504dae1 100644 --- a/tests/ui/typeck/typeck_type_placeholder_item.stderr +++ b/tests/ui/typeck/typeck_type_placeholder_item.stderr @@ -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 |
