properly check associated consts for infer placeholders

This commit is contained in:
Michael Goulet 2023-06-10 21:50:36 +00:00
parent 397641f3bd
commit 2b40268f8b
12 changed files with 77 additions and 34 deletions

View File

@ -666,17 +666,15 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
tcx.ensure().fn_sig(def_id); tcx.ensure().fn_sig(def_id);
} }
hir::TraitItemKind::Const(.., Some(_)) => { hir::TraitItemKind::Const(ty, body_id) => {
tcx.ensure().type_of(def_id); tcx.ensure().type_of(def_id);
} if !tcx.sess.diagnostic().has_stashed_diagnostic(ty.span, StashKey::ItemNoType)
&& !(is_suggestable_infer_ty(ty) && body_id.is_some())
hir::TraitItemKind::Const(hir_ty, _) => { {
tcx.ensure().type_of(def_id); // Account for `const C: _;`.
// Account for `const C: _;`. let mut visitor = HirPlaceholderCollector::default();
let mut visitor = HirPlaceholderCollector::default(); visitor.visit_trait_item(trait_item);
visitor.visit_trait_item(trait_item); placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
if !tcx.sess.diagnostic().has_stashed_diagnostic(hir_ty.span, StashKey::ItemNoType) {
placeholder_type_error(tcx, None, visitor.0, false, None, "constant");
} }
} }
@ -721,7 +719,14 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type"); placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
} }
hir::ImplItemKind::Const(..) => {} hir::ImplItemKind::Const(ty, _) => {
// Account for `const T: _ = ..;`
if !is_suggestable_infer_ty(ty) {
let mut visitor = HirPlaceholderCollector::default();
visitor.visit_impl_item(impl_item);
placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
}
}
} }
} }

View File

@ -341,7 +341,12 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
.and_then(|body_id| { .and_then(|body_id| {
is_suggestable_infer_ty(ty).then(|| { is_suggestable_infer_ty(ty).then(|| {
infer_placeholder_type( infer_placeholder_type(
tcx, def_id, body_id, ty.span, item.ident, "constant", tcx,
def_id,
body_id,
ty.span,
item.ident,
"associated constant",
) )
}) })
}) })
@ -359,7 +364,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
} }
ImplItemKind::Const(ty, body_id) => { ImplItemKind::Const(ty, body_id) => {
if is_suggestable_infer_ty(ty) { if is_suggestable_infer_ty(ty) {
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident, "constant") infer_placeholder_type(
tcx,
def_id,
body_id,
ty.span,
item.ident,
"associated constant",
)
} else { } else {
icx.to_ty(ty) icx.to_ty(ty)
} }

View File

@ -0,0 +1,10 @@
trait Trait {
const ASSOC: i32;
}
impl Trait for () {
const ASSOC: &dyn Fn(_) = 1i32;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
}
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/infer-placeholder-in-non-suggestable-pos.rs:6:26
|
LL | const ASSOC: &dyn Fn(_) = 1i32;
| ^ not allowed in type signatures
error: aborting due to previous error
For more information about this error, try `rustc --explain E0121`.

View File

@ -33,15 +33,15 @@ static TY_STATIC_MIXED: Bar<_, _> = Bar::<i32, 3>(0);
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
trait ArrAssocConst { trait ArrAssocConst {
const ARR: [u8; _]; const ARR: [u8; _];
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
} }
trait TyAssocConst { trait TyAssocConst {
const ARR: Bar<i32, _>; const ARR: Bar<i32, _>;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
} }
trait TyAssocConstMixed { trait TyAssocConstMixed {
const ARR: Bar<_, _>; const ARR: Bar<_, _>;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
} }
trait AssocTy { trait AssocTy {

View File

@ -74,19 +74,19 @@ LL | static TY_STATIC_MIXED: Bar<_, _> = Bar::<i32, 3>(0);
| not allowed in type signatures | not allowed in type signatures
| help: replace with the correct type: `Bar<i32, 3>` | help: replace with the correct type: `Bar<i32, 3>`
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/in-signature.rs:35:21 --> $DIR/in-signature.rs:35:21
| |
LL | const ARR: [u8; _]; LL | const ARR: [u8; _];
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/in-signature.rs:39:25 --> $DIR/in-signature.rs:39:25
| |
LL | const ARR: Bar<i32, _>; LL | const ARR: Bar<i32, _>;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/in-signature.rs:43:20 --> $DIR/in-signature.rs:43:20
| |
LL | const ARR: Bar<_, _>; LL | const ARR: Bar<_, _>;

View File

@ -3,12 +3,13 @@ struct MyStruct;
trait Test { trait Test {
const TEST: fn() -> _; const TEST: fn() -> _;
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for functions [E0121] //~^ ERROR: the placeholder `_` is not allowed within types on item signatures for functions [E0121]
//~| ERROR: the placeholder `_` is not allowed within types on item signatures for constants [E0121] //~| ERROR: the placeholder `_` is not allowed within types on item signatures for associated constants [E0121]
} }
impl Test for MyStruct { impl Test for MyStruct {
const TEST: fn() -> _ = 42; const TEST: fn() -> _ = 42;
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for functions [E0121] //~^ ERROR: the placeholder `_` is not allowed within types on item signatures for functions [E0121]
//~| ERROR: the placeholder `_` is not allowed within types on item signatures for associated constants [E0121]
} }
fn main() {} fn main() {}

View File

@ -4,7 +4,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
LL | const TEST: fn() -> _; LL | const TEST: fn() -> _;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/type-placeholder-fn-in-const.rs:4:25 --> $DIR/type-placeholder-fn-in-const.rs:4:25
| |
LL | const TEST: fn() -> _; LL | const TEST: fn() -> _;
@ -16,6 +16,12 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
LL | const TEST: fn() -> _ = 42; LL | const TEST: fn() -> _ = 42;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error: aborting due to 3 previous errors error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/type-placeholder-fn-in-const.rs:10:25
|
LL | const TEST: fn() -> _ = 42;
| ^ not allowed in type signatures
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0121`. For more information about this error, try `rustc --explain E0121`.

View File

@ -190,9 +190,9 @@ trait Qux {
type B = _; type B = _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
const C: _; const C: _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
const D: _ = 42; const D: _ = 42;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
// type E: _; // FIXME: make the parser propagate the existence of `B` // type E: _; // FIXME: make the parser propagate the existence of `B`
type F: std::ops::Fn(_); type F: std::ops::Fn(_);
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
@ -203,10 +203,10 @@ impl Qux for Struct {
type B = _; type B = _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
const C: _; const C: _;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
//~| ERROR associated constant in `impl` without body //~| ERROR associated constant in `impl` without body
const D: _ = 42; const D: _ = 42;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
} }
fn map<T>(_: fn() -> Option<&'static T>) -> Option<T> { fn map<T>(_: fn() -> Option<&'static T>) -> Option<T> {

View File

@ -525,13 +525,13 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
LL | type B = _; LL | type B = _;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item.rs:192:14 --> $DIR/typeck_type_placeholder_item.rs:192:14
| |
LL | const C: _; LL | const C: _;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item.rs:194:14 --> $DIR/typeck_type_placeholder_item.rs:194:14
| |
LL | const D: _ = 42; LL | const D: _ = 42;
@ -642,13 +642,13 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
LL | type B = _; LL | type B = _;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item.rs:205:14 --> $DIR/typeck_type_placeholder_item.rs:205:14
| |
LL | const C: _; LL | const C: _;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item.rs:208:14 --> $DIR/typeck_type_placeholder_item.rs:208:14
| |
LL | const D: _ = 42; LL | const D: _ = 42;

View File

@ -16,14 +16,14 @@ const TEST4: fn() -> _ = 42;
trait Test5 { trait Test5 {
const TEST5: _ = 42; const TEST5: _ = 42;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
} }
struct Test6; struct Test6;
impl Test6 { impl Test6 {
const TEST6: _ = 13; const TEST6: _ = 13;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
} }
pub fn main() { pub fn main() {

View File

@ -37,7 +37,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
LL | const TEST4: fn() -> _ = 42; LL | const TEST4: fn() -> _ = 42;
| ^ not allowed in type signatures | ^ not allowed in type signatures
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item_help.rs:18:18 --> $DIR/typeck_type_placeholder_item_help.rs:18:18
| |
LL | const TEST5: _ = 42; LL | const TEST5: _ = 42;
@ -46,7 +46,7 @@ LL | const TEST5: _ = 42;
| not allowed in type signatures | not allowed in type signatures
| help: replace with the correct type: `i32` | help: replace with the correct type: `i32`
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
--> $DIR/typeck_type_placeholder_item_help.rs:25:18 --> $DIR/typeck_type_placeholder_item_help.rs:25:18
| |
LL | const TEST6: _ = 13; LL | const TEST6: _ = 13;