Skip to content

Commit

Permalink
Auto merge of rust-lang#124183 - compiler-errors:unnecessary-by-ref, …
Browse files Browse the repository at this point in the history
…r=<try>

Stop taking `ParamTy`/`ParamConst`/`EarlyParamRegion`/`AliasTy` by ref

It's unnecessary and is annoying when we have it by value.
  • Loading branch information
bors committed Apr 20, 2024
2 parents f9b1614 + 86756c1 commit 43c2ad0
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
.into_iter()
.map(|err| match err.obligation.predicate.kind().skip_binder() {
PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
match predicate.self_ty().kind() {
match *predicate.self_ty().kind() {
ty::Param(param_ty) => Ok((
generics.type_param(param_ty, tcx),
predicate.trait_ref.print_only_trait_path().to_string(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// LL | blk();
// | ----- this value implements `FnOnce`, which causes it to be moved when called
// ```
if let ty::Param(param_ty) = self_ty.kind()
if let ty::Param(param_ty) = *self_ty.kind()
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
&& let param = generics.type_param(param_ty, self.infcx.tcx)
&& let Some(hir_generics) = self
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {
let generics = self.tcx.generics_of(self.body_id);
let generic_param = generics.type_param(&param, self.tcx);
let generic_param = generics.type_param(param, self.tcx);
if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let callee_ty = callee_ty.peel_refs();
match *callee_ty.kind() {
ty::Param(param) => {
let param = self.tcx.generics_of(self.body_id).type_param(&param, self.tcx);
let param = self.tcx.generics_of(self.body_id).type_param(param, self.tcx);
if param.kind.is_synthetic() {
// if it's `impl Fn() -> ..` then just fall down to the def-id based logic
def_id = param.def_id;
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let Some(arg_ty) = args[0].as_type() else {
return false;
};
let ty::Param(param) = arg_ty.kind() else {
let ty::Param(param) = *arg_ty.kind() else {
return false;
};
// Is `generic_param` the same as the arg for this trait predicate?
generic_param.index == generics.type_param(&param, tcx).index
generic_param.index == generics.type_param(param, tcx).index
} else {
false
}
Expand All @@ -156,10 +156,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}

match ty.peel_refs().kind() {
match *ty.peel_refs().kind() {
ty::Param(param) => {
let generics = self.tcx.generics_of(self.body_id);
let generic_param = generics.type_param(&param, self.tcx);
let generic_param = generics.type_param(param, self.tcx);
for unsatisfied in unsatisfied_predicates.iter() {
// The parameter implements `IntoIterator`
// but it has called a method that requires it to implement `Iterator`
Expand Down Expand Up @@ -3232,9 +3232,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
candidates.dedup();

let param_type = match rcvr_ty.kind() {
let param_type = match *rcvr_ty.kind() {
ty::Param(param) => Some(param),
ty::Ref(_, ty, _) => match ty.kind() {
ty::Ref(_, ty, _) => match *ty.kind() {
ty::Param(param) => Some(param),
_ => None,
},
Expand Down Expand Up @@ -3429,7 +3429,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
param_type.map_or_else(
|| "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
ToString::to_string,
|p| p.to_string(),
)
},
},
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

// type_param_sugg_span is (span, has_bounds)
let (type_scope, type_param_sugg_span) = match bound_kind {
GenericKind::Param(ref param) => {
GenericKind::Param(param) => {
let generics = self.tcx.generics_of(generic_param_scope);
let def_id = generics.type_param(param, self.tcx).def_id.expect_local();
let scope = self.tcx.local_def_id_to_hir_id(def_id).owner.def_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

match err {
ArgumentSorts(values, _) | Sorts(values) => {
match (values.expected.kind(), values.found.kind()) {
match (*values.expected.kind(), *values.found.kind()) {
(ty::Closure(..), ty::Closure(..)) => {
diag.note("no two closures, even if identical, have the same type");
diag.help("consider boxing your closure and/or using it as a trait object");
Expand Down Expand Up @@ -452,7 +452,7 @@ impl<T> Trait<T> for X {
}
(ty::FnPtr(sig), ty::FnDef(def_id, _))
| (ty::FnDef(def_id, _), ty::FnPtr(sig)) => {
if tcx.fn_sig(*def_id).skip_binder().unsafety() < sig.unsafety() {
if tcx.fn_sig(def_id).skip_binder().unsafety() < sig.unsafety() {
diag.note(
"unsafe functions cannot be coerced into safe function pointers",
);
Expand Down Expand Up @@ -527,7 +527,7 @@ impl<T> Trait<T> for X {
diag: &mut Diag<'_>,
msg: impl Fn() -> String,
body_owner_def_id: DefId,
proj_ty: &ty::AliasTy<'tcx>,
proj_ty: ty::AliasTy<'tcx>,
ty: Ty<'tcx>,
) -> bool {
let tcx = self.tcx;
Expand All @@ -541,7 +541,7 @@ impl<T> Trait<T> for X {
};
// Get the `DefId` for the type parameter corresponding to `A` in `<A as T>::Foo`.
// This will also work for `impl Trait`.
let ty::Param(param_ty) = proj_ty.self_ty().kind() else {
let ty::Param(param_ty) = *proj_ty.self_ty().kind() else {
return false;
};
let generics = tcx.generics_of(body_owner_def_id);
Expand Down Expand Up @@ -598,7 +598,7 @@ impl<T> Trait<T> for X {
fn expected_projection(
&self,
diag: &mut Diag<'_>,
proj_ty: &ty::AliasTy<'tcx>,
proj_ty: ty::AliasTy<'tcx>,
values: ExpectedFound<Ty<'tcx>>,
body_owner_def_id: DefId,
cause_code: &ObligationCauseCode<'_>,
Expand Down Expand Up @@ -709,7 +709,7 @@ fn foo(&self) -> Self::T { String::new() }
&self,
diag: &mut Diag<'_>,
msg: impl Fn() -> String,
proj_ty: &ty::AliasTy<'tcx>,
proj_ty: ty::AliasTy<'tcx>,
ty: Ty<'tcx>,
) -> bool {
let tcx = self.tcx;
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl<'tcx> Generics {
/// Returns the `GenericParamDef` associated with this `EarlyParamRegion`.
pub fn region_param(
&'tcx self,
param: &ty::EarlyParamRegion,
param: ty::EarlyParamRegion,
tcx: TyCtxt<'tcx>,
) -> &'tcx GenericParamDef {
let param = self.param_at(param.index as usize, tcx);
Expand All @@ -274,7 +274,7 @@ impl<'tcx> Generics {
}

/// Returns the `GenericParamDef` associated with this `ParamTy`.
pub fn type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
pub fn type_param(&'tcx self, param: ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
let param = self.param_at(param.index as usize, tcx);
match param.kind {
GenericParamDefKind::Type { .. } => param,
Expand All @@ -286,7 +286,7 @@ impl<'tcx> Generics {
/// `Generics`.
pub fn opt_type_param(
&'tcx self,
param: &ParamTy,
param: ParamTy,
tcx: TyCtxt<'tcx>,
) -> Option<&'tcx GenericParamDef> {
let param = self.opt_param_at(param.index as usize, tcx)?;
Expand All @@ -297,7 +297,7 @@ impl<'tcx> Generics {
}

/// Returns the `GenericParamDef` associated with this `ParamConst`.
pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
pub fn const_param(&'tcx self, param: ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
let param = self.param_at(param.index as usize, tcx);
match param.kind {
GenericParamDefKind::Const { .. } => param,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {

fn pretty_print_inherent_projection(
&mut self,
alias_ty: &ty::AliasTy<'tcx>,
alias_ty: ty::AliasTy<'tcx>,
) -> Result<(), PrintError> {
let def_key = self.tcx().def_key(alias_ty.def_id);
self.path_generic_args(
Expand Down Expand Up @@ -3204,7 +3204,7 @@ define_print_and_forward_display! {

ty::AliasTy<'tcx> {
if let DefKind::Impl { of_trait: false } = cx.tcx().def_kind(cx.tcx().parent(self.def_id)) {
p!(pretty_print_inherent_projection(self))
p!(pretty_print_inherent_projection(*self))
} else {
// If we're printing verbosely, or don't want to invoke queries
// (`is_impl_trait_in_trait`), then fall back to printing the def path.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ impl<'tcx> ParamTy {
Ty::new_param(tcx, self.index, self.name)
}

pub fn span_from_generics(&self, tcx: TyCtxt<'tcx>, item_with_generics: DefId) -> Span {
pub fn span_from_generics(self, tcx: TyCtxt<'tcx>, item_with_generics: DefId) -> Span {
let generics = tcx.generics_of(item_with_generics);
let type_param = generics.type_param(self, tcx);
tcx.def_span(type_param.def_id)
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,19 +432,19 @@ impl<'tcx> TyCtxt<'tcx> {
.filter(|&(_, k)| {
match k.unpack() {
GenericArgKind::Lifetime(region) => match region.kind() {
ty::ReEarlyParam(ref ebr) => {
ty::ReEarlyParam(ebr) => {
!impl_generics.region_param(ebr, self).pure_wrt_drop
}
// Error: not a region param
_ => false,
},
GenericArgKind::Type(ty) => match ty.kind() {
ty::Param(ref pt) => !impl_generics.type_param(pt, self).pure_wrt_drop,
GenericArgKind::Type(ty) => match *ty.kind() {
ty::Param(pt) => !impl_generics.type_param(pt, self).pure_wrt_drop,
// Error: not a type param
_ => false,
},
GenericArgKind::Const(ct) => match ct.kind() {
ty::ConstKind::Param(ref pc) => {
ty::ConstKind::Param(pc) => {
!impl_generics.const_param(pc, self).pure_wrt_drop
}
// Error: not a const param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
msg: &str,
err: &mut Diag<'_, G>,
fn_sig: Option<&hir::FnSig<'_>>,
projection: Option<&ty::AliasTy<'_>>,
projection: Option<ty::AliasTy<'_>>,
trait_pred: ty::PolyTraitPredicate<'tcx>,
// When we are dealing with a trait, `super_traits` will be `Some`:
// Given `trait T: A + B + C {}`
Expand All @@ -142,7 +142,7 @@ pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
let generics = tcx.generics_of(item_id);
// Given `fn foo(t: impl Trait)` where `Trait` requires assoc type `A`...
if let Some((param, bound_str, fn_sig)) =
fn_sig.zip(projection).and_then(|(sig, p)| match p.self_ty().kind() {
fn_sig.zip(projection).and_then(|(sig, p)| match *p.self_ty().kind() {
// Shenanigans to get the `Trait` from the `impl Trait`.
ty::Param(param) => {
let param_def = generics.type_param(param, tcx);
Expand Down Expand Up @@ -252,7 +252,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);

let self_ty = trait_pred.skip_binder().self_ty();
let (param_ty, projection) = match self_ty.kind() {
let (param_ty, projection) = match *self_ty.kind() {
ty::Param(_) => (true, None),
ty::Alias(ty::Projection, projection) => (false, Some(projection)),
_ => (false, None),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_trait_selection/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn is_impossible_associated_item(
type Result = ControlFlow<()>;
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
// If this is a parameter from the trait item's own generics, then bail
if let ty::Param(param) = t.kind()
if let ty::Param(param) = *t.kind()
&& let param_def_id = self.generics.type_param(param, self.tcx).def_id
&& self.tcx.parent(param_def_id) == self.trait_item_def_id
{
Expand All @@ -492,7 +492,7 @@ fn is_impossible_associated_item(
}
fn visit_region(&mut self, r: ty::Region<'tcx>) -> Self::Result {
if let ty::ReEarlyParam(param) = r.kind()
&& let param_def_id = self.generics.region_param(&param, self.tcx).def_id
&& let param_def_id = self.generics.region_param(param, self.tcx).def_id
&& self.tcx.parent(param_def_id) == self.trait_item_def_id
{
return ControlFlow::Break(());
Expand All @@ -501,7 +501,7 @@ fn is_impossible_associated_item(
}
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> Self::Result {
if let ty::ConstKind::Param(param) = ct.kind()
&& let param_def_id = self.generics.const_param(&param, self.tcx).def_id
&& let param_def_id = self.generics.const_param(param, self.tcx).def_id
&& self.tcx.parent(param_def_id) == self.trait_item_def_id
{
return ControlFlow::Break(());
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ty_utils/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
TaitInBodyFinder { collector: self }.visit_expr(body);
}

fn visit_opaque_ty(&mut self, alias_ty: &ty::AliasTy<'tcx>) {
fn visit_opaque_ty(&mut self, alias_ty: ty::AliasTy<'tcx>) {
if !self.seen.insert(alias_ty.def_id.expect_local()) {
return;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
#[instrument(skip(self), ret, level = "trace")]
fn visit_ty(&mut self, t: Ty<'tcx>) {
t.super_visit_with(self);
match t.kind() {
match *t.kind() {
ty::Alias(ty::Opaque, alias_ty) if alias_ty.def_id.is_local() => {
self.visit_opaque_ty(alias_ty);
}
Expand Down Expand Up @@ -279,7 +279,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
// assumption to the `param_env` of the default method. We also separately
// rely on that assumption here.
let ty = self.tcx.type_of(alias_ty.def_id).instantiate(self.tcx, alias_ty.args);
let ty::Alias(ty::Opaque, alias_ty) = ty.kind() else { bug!("{ty:?}") };
let ty::Alias(ty::Opaque, alias_ty) = *ty.kind() else { bug!("{ty:?}") };
self.visit_opaque_ty(alias_ty);
}
}
Expand Down

0 comments on commit 43c2ad0

Please sign in to comment.