Migrate rustc_ty_utils to use SessionDiagnostic

This commit is contained in:
Peter Medus 2022-08-19 00:04:31 +01:00
parent 8a13871b69
commit 01c1616b25
8 changed files with 209 additions and 95 deletions

View File

@ -4601,6 +4601,7 @@ dependencies = [
"rustc_hir", "rustc_hir",
"rustc_index", "rustc_index",
"rustc_infer", "rustc_infer",
"rustc_macros",
"rustc_middle", "rustc_middle",
"rustc_session", "rustc_session",
"rustc_span", "rustc_span",

View File

@ -0,0 +1,47 @@
ty_utils_needs_drop_overflow = overflow while checking whether `{$query_ty}` requires drop
ty_utils_generic_constant_too_complex = overly complex generic constant
.help = consider moving this anonymous constant into a `const` function
.maybe_supported = this operation may be supported in the future
ty_utils_borrow_not_supported = borrowing is not supported in generic constants
ty_utils_address_and_deref_not_supported = dereferencing or taking the address is not supported in generic constants
ty_utils_array_not_supported = array construction is not supported in generic constants
ty_utils_block_not_supported = blocks are not supported in generic constant
ty_utils_never_to_any_not_supported = converting nevers to any is not supported in generic constant
ty_utils_tuple_not_supported = tuple construction is not supported in generic constants
ty_utils_index_not_supported = indexing is not supported in generic constant
ty_utils_field_not_supported = field access is not supported in generic constant
ty_utils_const_block_not_supported = const blocks are not supported in generic constant
ty_utils_adt_not_supported = struct/enum construction is not supported in generic constants
ty_utils_pointer_not_supported = pointer casts are not allowed in generic constants
ty_utils_yield_not_supported = generator control flow is not allowed in generic constants
ty_utils_loop_not_supported = loops and loop control flow are not supported in generic constants
ty_utils_box_not_supported = allocations are not allowed in generic constants
ty_utils_binary_not_supported = unsupported binary operation in generic constants
ty_utils_logical_op_not_supported = unsupported operation in generic constants, short-circuiting operations would imply control flow
ty_utils_assign_not_supported = assignment is not supported in generic constants
ty_utils_closure_and_return_not_supported = closures and function keywords are not supported in generic constants
ty_utils_control_flow_not_supported = control flow is not supported in generic constants
ty_utils_inline_asm_not_supported = assembly is not supported in generic constants
ty_utils_operation_not_supported = unsupported operation in generic constant

View File

@ -44,6 +44,7 @@ fluent_messages! {
plugin_impl => "../locales/en-US/plugin_impl.ftl", plugin_impl => "../locales/en-US/plugin_impl.ftl",
privacy => "../locales/en-US/privacy.ftl", privacy => "../locales/en-US/privacy.ftl",
save_analysis => "../locales/en-US/save_analysis.ftl", save_analysis => "../locales/en-US/save_analysis.ftl",
ty_utils => "../locales/en-US/ty_utils.ftl",
typeck => "../locales/en-US/typeck.ftl", typeck => "../locales/en-US/typeck.ftl",
} }

View File

@ -10,6 +10,7 @@ rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" } rustc_errors = { path = "../rustc_errors" }
rustc_hir = { path = "../rustc_hir" } rustc_hir = { path = "../rustc_hir" }
rustc_infer = { path = "../rustc_infer" } rustc_infer = { path = "../rustc_infer" }
rustc_macros = { path = "../rustc_macros" }
rustc_span = { path = "../rustc_span" } rustc_span = { path = "../rustc_span" }
rustc_session = { path = "../rustc_session" } rustc_session = { path = "../rustc_session" }
rustc_target = { path = "../rustc_target" } rustc_target = { path = "../rustc_target" }

View File

@ -11,6 +11,8 @@ use rustc_target::abi::VariantIdx;
use std::iter; use std::iter;
use crate::errors::{GenericConstantTooComplex, GenericConstantTooComplexSub};
/// Destructures array, ADT or tuple constants into the constants /// Destructures array, ADT or tuple constants into the constants
/// of their fields. /// of their fields.
pub(crate) fn destructure_const<'tcx>( pub(crate) fn destructure_const<'tcx>(
@ -93,26 +95,25 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
self.body.exprs[self.body_id].span self.body.exprs[self.body_id].span
} }
fn error(&mut self, span: Span, msg: &str) -> Result<!, ErrorGuaranteed> { fn error(&mut self, sub: GenericConstantTooComplexSub) -> Result<!, ErrorGuaranteed> {
let reported = self let reported = self.tcx.sess.emit_err(GenericConstantTooComplex {
.tcx span: self.root_span(),
.sess maybe_supported: None,
.struct_span_err(self.root_span(), "overly complex generic constant") sub,
.span_label(span, msg) });
.help("consider moving this anonymous constant into a `const` function")
.emit();
Err(reported) Err(reported)
} }
fn maybe_supported_error(&mut self, span: Span, msg: &str) -> Result<!, ErrorGuaranteed> {
let reported = self fn maybe_supported_error(
.tcx &mut self,
.sess sub: GenericConstantTooComplexSub,
.struct_span_err(self.root_span(), "overly complex generic constant") ) -> Result<!, ErrorGuaranteed> {
.span_label(span, msg) let reported = self.tcx.sess.emit_err(GenericConstantTooComplex {
.help("consider moving this anonymous constant into a `const` function") span: self.root_span(),
.note("this operation may be supported in the future") maybe_supported: Some(()),
.emit(); sub,
});
Err(reported) Err(reported)
} }
@ -243,22 +244,23 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
&ExprKind::Scope { value, .. } => self.recurse_build(value)?, &ExprKind::Scope { value, .. } => self.recurse_build(value)?,
&ExprKind::PlaceTypeAscription { source, .. } &ExprKind::PlaceTypeAscription { source, .. }
| &ExprKind::ValueTypeAscription { source, .. } => self.recurse_build(source)?, | &ExprKind::ValueTypeAscription { source, .. } => self.recurse_build(source)?,
&ExprKind::Literal { lit, neg} => { &ExprKind::Literal { lit, neg } => {
let sp = node.span; let sp = node.span;
let constant = let constant = match self.tcx.at(sp).lit_to_const(LitToConstInput {
match self.tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) { lit: &lit.node,
Ok(c) => c, ty: node.ty,
Err(LitToConstError::Reported) => { neg,
self.tcx.const_error(node.ty) }) {
} Ok(c) => c,
Err(LitToConstError::TypeError) => { Err(LitToConstError::Reported) => self.tcx.const_error(node.ty),
bug!("encountered type error in lit_to_const") Err(LitToConstError::TypeError) => {
} bug!("encountered type error in lit_to_const")
}; }
};
self.nodes.push(Node::Leaf(constant)) self.nodes.push(Node::Leaf(constant))
} }
&ExprKind::NonHirLiteral { lit , user_ty: _} => { &ExprKind::NonHirLiteral { lit, user_ty: _ } => {
let val = ty::ValTree::from_scalar_int(lit); let val = ty::ValTree::from_scalar_int(lit);
self.nodes.push(Node::Leaf(ty::Const::from_value(self.tcx, val, node.ty))) self.nodes.push(Node::Leaf(ty::Const::from_value(self.tcx, val, node.ty)))
} }
@ -269,19 +271,17 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => { &ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs); let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs);
let constant = self.tcx.mk_const(ty::ConstS { let constant = self
kind: ty::ConstKind::Unevaluated(uneval), .tcx
ty: node.ty, .mk_const(ty::ConstS { kind: ty::ConstKind::Unevaluated(uneval), ty: node.ty });
});
self.nodes.push(Node::Leaf(constant)) self.nodes.push(Node::Leaf(constant))
} }
ExprKind::ConstParam {param, ..} => { ExprKind::ConstParam { param, .. } => {
let const_param = self.tcx.mk_const(ty::ConstS { let const_param = self
kind: ty::ConstKind::Param(*param), .tcx
ty: node.ty, .mk_const(ty::ConstS { kind: ty::ConstKind::Param(*param), ty: node.ty });
});
self.nodes.push(Node::Leaf(const_param)) self.nodes.push(Node::Leaf(const_param))
} }
@ -312,13 +312,13 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
// } // }
// ``` // ```
ExprKind::Block { block } => { ExprKind::Block { block } => {
if let thir::Block { stmts: box [], expr: Some(e), .. } = &self.body.blocks[*block] { if let thir::Block { stmts: box [], expr: Some(e), .. } = &self.body.blocks[*block]
{
self.recurse_build(*e)? self.recurse_build(*e)?
} else { } else {
self.maybe_supported_error( self.maybe_supported_error(GenericConstantTooComplexSub::BlockNotSupported(
node.span, node.span,
"blocks are not supported in generic constant", ))?
)?
} }
} }
// `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a // `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
@ -332,7 +332,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
let arg = self.recurse_build(source)?; let arg = self.recurse_build(source)?;
self.nodes.push(Node::Cast(CastKind::As, arg, node.ty)) self.nodes.push(Node::Cast(CastKind::As, arg, node.ty))
} }
ExprKind::Borrow{ arg, ..} => { ExprKind::Borrow { arg, .. } => {
let arg_node = &self.body.exprs[*arg]; let arg_node = &self.body.exprs[*arg];
// Skip reborrows for now until we allow Deref/Borrow/AddressOf // Skip reborrows for now until we allow Deref/Borrow/AddressOf
@ -341,76 +341,69 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
if let ExprKind::Deref { arg } = arg_node.kind { if let ExprKind::Deref { arg } = arg_node.kind {
self.recurse_build(arg)? self.recurse_build(arg)?
} else { } else {
self.maybe_supported_error( self.maybe_supported_error(GenericConstantTooComplexSub::BorrowNotSupported(
node.span, node.span,
"borrowing is not supported in generic constants", ))?
)?
} }
} }
// FIXME(generic_const_exprs): We may want to support these. // FIXME(generic_const_exprs): We may want to support these.
ExprKind::AddressOf { .. } | ExprKind::Deref {..}=> self.maybe_supported_error( ExprKind::AddressOf { .. } | ExprKind::Deref { .. } => self.maybe_supported_error(
node.span, GenericConstantTooComplexSub::AddressAndDerefNotSupported(node.span),
"dereferencing or taking the address is not supported in generic constants",
)?, )?,
ExprKind::Repeat { .. } | ExprKind::Array { .. } => self.maybe_supported_error( ExprKind::Repeat { .. } | ExprKind::Array { .. } => self.maybe_supported_error(
node.span, GenericConstantTooComplexSub::ArrayNotSupported(node.span),
"array construction is not supported in generic constants",
)?, )?,
ExprKind::NeverToAny { .. } => self.maybe_supported_error( ExprKind::NeverToAny { .. } => self.maybe_supported_error(
node.span, GenericConstantTooComplexSub::NeverToAnyNotSupported(node.span),
"converting nevers to any is not supported in generic constant",
)?, )?,
ExprKind::Tuple { .. } => self.maybe_supported_error( ExprKind::Tuple { .. } => self.maybe_supported_error(
node.span, GenericConstantTooComplexSub::TupleNotSupported(node.span),
"tuple construction is not supported in generic constants",
)?, )?,
ExprKind::Index { .. } => self.maybe_supported_error( ExprKind::Index { .. } => self.maybe_supported_error(
node.span, GenericConstantTooComplexSub::IndexNotSupported(node.span),
"indexing is not supported in generic constant",
)?, )?,
ExprKind::Field { .. } => self.maybe_supported_error( ExprKind::Field { .. } => self.maybe_supported_error(
node.span, GenericConstantTooComplexSub::FieldNotSupported(node.span),
"field access is not supported in generic constant",
)?, )?,
ExprKind::ConstBlock { .. } => self.maybe_supported_error( ExprKind::ConstBlock { .. } => self.maybe_supported_error(
node.span, GenericConstantTooComplexSub::ConstBlockNotSupported(node.span),
"const blocks are not supported in generic constant",
)?,
ExprKind::Adt(_) => self.maybe_supported_error(
node.span,
"struct/enum construction is not supported in generic constants",
)?, )?,
ExprKind::Adt(_) => self
.maybe_supported_error(GenericConstantTooComplexSub::AdtNotSupported(node.span))?,
// dont know if this is correct // dont know if this is correct
ExprKind::Pointer { .. } => ExprKind::Pointer { .. } => {
self.error(node.span, "pointer casts are not allowed in generic constants")?, self.error(GenericConstantTooComplexSub::PointerNotSupported(node.span))?
ExprKind::Yield { .. } => }
self.error(node.span, "generator control flow is not allowed in generic constants")?, ExprKind::Yield { .. } => {
ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Loop { .. } => self self.error(GenericConstantTooComplexSub::YieldNotSupported(node.span))?
.error( }
node.span, ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Loop { .. } => {
"loops and loop control flow are not supported in generic constants", self.error(GenericConstantTooComplexSub::LoopNotSupported(node.span))?
)?, }
ExprKind::Box { .. } => ExprKind::Box { .. } => {
self.error(node.span, "allocations are not allowed in generic constants")?, self.error(GenericConstantTooComplexSub::BoxNotSupported(node.span))?
}
ExprKind::Unary { .. } => unreachable!(), ExprKind::Unary { .. } => unreachable!(),
// we handle valid unary/binary ops above // we handle valid unary/binary ops above
ExprKind::Binary { .. } => ExprKind::Binary { .. } => {
self.error(node.span, "unsupported binary operation in generic constants")?, self.error(GenericConstantTooComplexSub::BinaryNotSupported(node.span))?
ExprKind::LogicalOp { .. } => }
self.error(node.span, "unsupported operation in generic constants, short-circuiting operations would imply control flow")?, ExprKind::LogicalOp { .. } => {
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => { self.error(GenericConstantTooComplexSub::LogicalOpNotSupported(node.span))?
self.error(node.span, "assignment is not supported in generic constants")? }
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
self.error(GenericConstantTooComplexSub::AssignNotSupported(node.span))?
}
ExprKind::Closure { .. } | ExprKind::Return { .. } => {
self.error(GenericConstantTooComplexSub::ClosureAndReturnNotSupported(node.span))?
} }
ExprKind::Closure { .. } | ExprKind::Return { .. } => self.error(
node.span,
"closures and function keywords are not supported in generic constants",
)?,
// let expressions imply control flow // let expressions imply control flow
ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } => ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } => {
self.error(node.span, "control flow is not supported in generic constants")?, self.error(GenericConstantTooComplexSub::ControlFlowNotSupported(node.span))?
}
ExprKind::InlineAsm { .. } => { ExprKind::InlineAsm { .. } => {
self.error(node.span, "assembly is not supported in generic constants")? self.error(GenericConstantTooComplexSub::InlineAsmNotSupported(node.span))?
} }
// we dont permit let stmts so `VarRef` and `UpvarRef` cant happen // we dont permit let stmts so `VarRef` and `UpvarRef` cant happen
@ -418,7 +411,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
| ExprKind::UpvarRef { .. } | ExprKind::UpvarRef { .. }
| ExprKind::StaticRef { .. } | ExprKind::StaticRef { .. }
| ExprKind::ThreadLocalRef(_) => { | ExprKind::ThreadLocalRef(_) => {
self.error(node.span, "unsupported operation in generic constant")? self.error(GenericConstantTooComplexSub::OperationNotSupported(node.span))?
} }
}) })
} }

View File

@ -0,0 +1,69 @@
//! Errors emitted by ty_utils
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use rustc_middle::ty::Ty;
use rustc_span::Span;
#[derive(SessionDiagnostic)]
#[diag(ty_utils::needs_drop_overflow)]
pub struct NeedsDropOverflow<'tcx> {
pub query_ty: Ty<'tcx>,
}
#[derive(SessionDiagnostic)]
#[diag(ty_utils::generic_constant_too_complex)]
#[help]
pub struct GenericConstantTooComplex {
#[primary_span]
pub span: Span,
#[note(ty_utils::maybe_supported)]
pub maybe_supported: Option<()>,
#[subdiagnostic]
pub sub: GenericConstantTooComplexSub,
}
#[derive(SessionSubdiagnostic)]
pub enum GenericConstantTooComplexSub {
#[label(ty_utils::borrow_not_supported)]
BorrowNotSupported(#[primary_span] Span),
#[label(ty_utils::address_and_deref_not_supported)]
AddressAndDerefNotSupported(#[primary_span] Span),
#[label(ty_utils::array_not_supported)]
ArrayNotSupported(#[primary_span] Span),
#[label(ty_utils::block_not_supported)]
BlockNotSupported(#[primary_span] Span),
#[label(ty_utils::never_to_any_not_supported)]
NeverToAnyNotSupported(#[primary_span] Span),
#[label(ty_utils::tuple_not_supported)]
TupleNotSupported(#[primary_span] Span),
#[label(ty_utils::index_not_supported)]
IndexNotSupported(#[primary_span] Span),
#[label(ty_utils::field_not_supported)]
FieldNotSupported(#[primary_span] Span),
#[label(ty_utils::const_block_not_supported)]
ConstBlockNotSupported(#[primary_span] Span),
#[label(ty_utils::adt_not_supported)]
AdtNotSupported(#[primary_span] Span),
#[label(ty_utils::pointer_not_supported)]
PointerNotSupported(#[primary_span] Span),
#[label(ty_utils::yield_not_supported)]
YieldNotSupported(#[primary_span] Span),
#[label(ty_utils::loop_not_supported)]
LoopNotSupported(#[primary_span] Span),
#[label(ty_utils::box_not_supported)]
BoxNotSupported(#[primary_span] Span),
#[label(ty_utils::binary_not_supported)]
BinaryNotSupported(#[primary_span] Span),
#[label(ty_utils::logical_op_not_supported)]
LogicalOpNotSupported(#[primary_span] Span),
#[label(ty_utils::assign_not_supported)]
AssignNotSupported(#[primary_span] Span),
#[label(ty_utils::closure_and_return_not_supported)]
ClosureAndReturnNotSupported(#[primary_span] Span),
#[label(ty_utils::control_flow_not_supported)]
ControlFlowNotSupported(#[primary_span] Span),
#[label(ty_utils::inline_asm_not_supported)]
InlineAsmNotSupported(#[primary_span] Span),
#[label(ty_utils::operation_not_supported)]
OperationNotSupported(#[primary_span] Span),
}

View File

@ -10,6 +10,8 @@
#![feature(never_type)] #![feature(never_type)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
#[macro_use] #[macro_use]
extern crate rustc_middle; extern crate rustc_middle;
@ -21,6 +23,7 @@ use rustc_middle::ty::query::Providers;
mod assoc; mod assoc;
mod common_traits; mod common_traits;
mod consts; mod consts;
mod errors;
mod implied_bounds; mod implied_bounds;
pub mod instance; pub mod instance;
mod needs_drop; mod needs_drop;

View File

@ -9,6 +9,8 @@ use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt};
use rustc_session::Limit; use rustc_session::Limit;
use rustc_span::{sym, DUMMY_SP}; use rustc_span::{sym, DUMMY_SP};
use crate::errors::NeedsDropOverflow;
type NeedsDropResult<T> = Result<T, AlwaysRequiresDrop>; type NeedsDropResult<T> = Result<T, AlwaysRequiresDrop>;
fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool { fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
@ -90,10 +92,7 @@ where
if !self.recursion_limit.value_within_limit(level) { if !self.recursion_limit.value_within_limit(level) {
// Not having a `Span` isn't great. But there's hopefully some other // Not having a `Span` isn't great. But there's hopefully some other
// recursion limit error as well. // recursion limit error as well.
tcx.sess.span_err( tcx.sess.emit_err(NeedsDropOverflow { query_ty: self.query_ty });
DUMMY_SP,
&format!("overflow while checking whether `{}` requires drop", self.query_ty),
);
return Some(Err(AlwaysRequiresDrop)); return Some(Err(AlwaysRequiresDrop));
} }