Skip to content

Commit

Permalink
Unrolled build for rust-lang#123130
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#123130 - oli-obk:missing_type_taint, r=compiler-errors

Load missing type of impl associated constant from trait definition

fixes rust-lang#123092

Also does some cleanups I discovered while analyzing this issue
  • Loading branch information
rust-timer authored Mar 28, 2024
2 parents 9d70954 + 86e750f commit b68d740
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 66 deletions.
110 changes: 57 additions & 53 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,6 @@ macro_rules! type_error_struct {
})
}

/// If this `DefId` is a "primary tables entry", returns
/// `Some((body_id, body_ty, fn_sig))`. Otherwise, returns `None`.
///
/// If this function returns `Some`, then `typeck_results(def_id)` will
/// succeed; if it returns `None`, then `typeck_results(def_id)` may or
/// may not succeed. In some cases where this function returns `None`
/// (notably closures), `typeck_results(def_id)` would wind up
/// redirecting to the owning function.
fn primary_body_of(
node: Node<'_>,
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
Some((node.body_id()?, node.ty(), node.fn_sig()))
}

fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
// Closures' typeck results come from their outermost function,
// as they are part of the same "inference environment".
Expand All @@ -106,7 +92,7 @@ fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
}

if let Some(def_id) = def_id.as_local() {
primary_body_of(tcx.hir_node_by_def_id(def_id)).is_some()
tcx.hir_node_by_def_id(def_id).body_id().is_some()
} else {
false
}
Expand Down Expand Up @@ -163,7 +149,7 @@ fn typeck_with_fallback<'tcx>(
let span = tcx.hir().span(id);

// Figure out what primary body this item has.
let (body_id, body_ty, fn_sig) = primary_body_of(node).unwrap_or_else(|| {
let body_id = node.body_id().unwrap_or_else(|| {
span_bug!(span, "can't type-check body of {:?}", def_id);
});
let body = tcx.hir().body(body_id);
Expand All @@ -176,7 +162,7 @@ fn typeck_with_fallback<'tcx>(
}
let mut fcx = FnCtxt::new(&root_ctxt, param_env, def_id);

if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
if let Some(hir::FnSig { header, decl, .. }) = node.fn_sig() {
let fn_sig = if decl.output.get_infer_ret_ty().is_some() {
fcx.lowerer().lower_fn_ty(id, header.unsafety, header.abi, decl, None, None)
} else {
Expand All @@ -191,42 +177,7 @@ 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,
}))
} else if let Node::AnonConst(_) = node {
match tcx.parent_hir_node(id) {
Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), .. })
if anon_const.hir_id == id =>
{
Some(fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
}))
}
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
asm.operands.iter().find_map(|(op, _op_sp)| match op {
hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => {
// Inline assembly constants must be integers.
Some(fcx.next_int_var())
}
hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => {
Some(fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
}))
}
_ => None,
})
}
_ => None,
}
} else {
None
};
let expected_type = infer_type_if_missing(&fcx, node);
let expected_type = expected_type.unwrap_or_else(fallback);

let expected_type = fcx.normalize(body.value.span, expected_type);
Expand Down Expand Up @@ -296,6 +247,59 @@ fn typeck_with_fallback<'tcx>(
typeck_results
}

fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Option<Ty<'tcx>> {
let tcx = fcx.tcx;
let def_id = fcx.body_id;
let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = node.ty() {
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 {
let id = tcx.local_def_id_to_hir_id(def_id);
match tcx.parent_hir_node(id) {
Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), span, .. })
if anon_const.hir_id == id =>
{
Some(fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
}))
}
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), span, .. })
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), span, .. }) => {
asm.operands.iter().find_map(|(op, _op_sp)| match op {
hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => {
// Inline assembly constants must be integers.
Some(fcx.next_int_var())
}
hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => {
Some(fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
}))
}
_ => None,
})
}
_ => None,
}
} else {
None
};
expected_type
}

/// When `check_fn` is invoked on a coroutine (i.e., a body that
/// includes yield), it returns back some information about the yield
/// points.
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/consts/missing_assoc_const_type.rs
Original file line number Diff line number Diff line change
@@ -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() {}
8 changes: 8 additions & 0 deletions tests/ui/consts/missing_assoc_const_type.stderr
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions tests/ui/consts/missing_assoc_const_type2.rs
Original file line number Diff line number Diff line change
@@ -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() {}
8 changes: 8 additions & 0 deletions tests/ui/consts/missing_assoc_const_type2.stderr
Original file line number Diff line number Diff line change
@@ -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

23 changes: 10 additions & 13 deletions tests/ui/typeck/typeck_type_placeholder_item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b68d740

Please sign in to comment.