Avoid using the HIR span in the happy path

This commit is contained in:
Oli Scherer 2024-02-13 09:43:33 +00:00
parent 40b3cb0eb1
commit 75de6cf9a3

View File

@ -15,7 +15,7 @@ use rustc_infer::infer::{DefineOpaqueTypes, TyCtxtInferExt};
use rustc_infer::traits::Obligation; use rustc_infer::traits::Obligation;
use rustc_middle::ty::adjustment::CoerceUnsizedInfo; use rustc_middle::ty::adjustment::CoerceUnsizedInfo;
use rustc_middle::ty::{self, suggest_constraining_type_params, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{self, suggest_constraining_type_params, Ty, TyCtxt, TypeVisitableExt};
use rustc_span::Span; use rustc_span::{Span, DUMMY_SP};
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
use rustc_trait_selection::traits::misc::{ use rustc_trait_selection::traits::misc::{
type_allowed_to_implement_const_param_ty, type_allowed_to_implement_copy, type_allowed_to_implement_const_param_ty, type_allowed_to_implement_copy,
@ -95,18 +95,20 @@ fn visit_implementation_of_copy(
if let ty::ImplPolarity::Negative = tcx.impl_polarity(impl_did) { if let ty::ImplPolarity::Negative = tcx.impl_polarity(impl_did) {
return Ok(()); return Ok(());
} }
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
let cause = traits::ObligationCause::misc(span, impl_did); let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did);
match type_allowed_to_implement_copy(tcx, param_env, self_type, cause) { match type_allowed_to_implement_copy(tcx, param_env, self_type, cause) {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(CopyImplementationError::InfringingFields(fields)) => { Err(CopyImplementationError::InfringingFields(fields)) => {
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
Err(infringing_fields_error(tcx, fields, LangItem::Copy, impl_did, span)) Err(infringing_fields_error(tcx, fields, LangItem::Copy, impl_did, span))
} }
Err(CopyImplementationError::NotAnAdt) => { Err(CopyImplementationError::NotAnAdt) => {
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
Err(tcx.dcx().emit_err(errors::CopyImplOnNonAdt { span })) Err(tcx.dcx().emit_err(errors::CopyImplOnNonAdt { span }))
} }
Err(CopyImplementationError::HasDestructor) => { Err(CopyImplementationError::HasDestructor) => {
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
Err(tcx.dcx().emit_err(errors::CopyImplOnTypeWithDtor { span })) Err(tcx.dcx().emit_err(errors::CopyImplOnTypeWithDtor { span }))
} }
} }
@ -124,15 +126,16 @@ fn visit_implementation_of_const_param_ty(
if let ty::ImplPolarity::Negative = tcx.impl_polarity(impl_did) { if let ty::ImplPolarity::Negative = tcx.impl_polarity(impl_did) {
return Ok(()); return Ok(());
} }
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
let cause = traits::ObligationCause::misc(span, impl_did); let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did);
match type_allowed_to_implement_const_param_ty(tcx, param_env, self_type, cause) { match type_allowed_to_implement_const_param_ty(tcx, param_env, self_type, cause) {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(ConstParamTyImplementationError::InfrigingFields(fields)) => { Err(ConstParamTyImplementationError::InfrigingFields(fields)) => {
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
Err(infringing_fields_error(tcx, fields, LangItem::ConstParamTy, impl_did, span)) Err(infringing_fields_error(tcx, fields, LangItem::ConstParamTy, impl_did, span))
} }
Err(ConstParamTyImplementationError::NotAnAdtOrBuiltinAllowed) => { Err(ConstParamTyImplementationError::NotAnAdtOrBuiltinAllowed) => {
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
Err(tcx.dcx().emit_err(errors::ConstParamTyImplOnNonAdt { span })) Err(tcx.dcx().emit_err(errors::ConstParamTyImplOnNonAdt { span }))
} }
} }