Skip to content

Rollup of 5 pull requests #140138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,15 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
let items: &AssocItems = self.tcx.associated_items(self.def_id);
items
.in_definition_order()
.filter(|item| item.is_type())
.filter(|item| {
!self
.gen_args
.constraints
.iter()
.any(|constraint| constraint.ident.name == item.name())
item.is_type()
&& !item.is_impl_trait_in_trait()
&& !self
.gen_args
.constraints
.iter()
.any(|constraint| constraint.ident.name == item.name())
})
.filter(|item| !item.is_impl_trait_in_trait())
.map(|item| self.tcx.item_ident(item.def_id).to_string())
.collect()
} else {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
};
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Namespace, Res};
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
Expand Down Expand Up @@ -1731,9 +1731,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
tcx.associated_items(*trait_def_id)
.in_definition_order()
.any(|i| {
i.namespace() == Namespace::TypeNS
i.is_type()
&& !i.is_impl_trait_in_trait()
&& i.ident(tcx).normalize_to_macros_2_0() == assoc_ident
&& i.is_type()
})
// Consider only accessible traits
&& tcx.visibility(*trait_def_id)
Expand Down
52 changes: 51 additions & 1 deletion compiler/rustc_hir_typeck/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// us do better coercions than we would be able to do otherwise,
// particularly for things like `String + &String`.
let rhs_ty_var = self.next_ty_var(rhs_expr.span);

let result = self.lookup_op_method(
(lhs_expr, lhs_ty),
Some((rhs_expr, rhs_ty_var)),
Expand Down Expand Up @@ -698,6 +697,57 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let lhs_name_str = match lhs_expr.kind {
hir::ExprKind::Path(hir::QPath::Resolved(_, path)) => {
path.segments.last().map_or("_".to_string(), |s| s.ident.to_string())
}
_ => self
.tcx
.sess
.source_map()
.span_to_snippet(lhs_expr.span)
.unwrap_or("_".to_string()),
};

if op.span().can_be_used_for_suggestions() {
match op {
Op::AssignOp(Spanned { node: hir::AssignOpKind::AddAssign, .. })
if lhs_ty.is_raw_ptr() && rhs_ty.is_integral() =>
{
err.multipart_suggestion(
"consider using `add` or `wrapping_add` to do pointer arithmetic",
vec![
(lhs_expr.span.shrink_to_lo(), format!("{} = ", lhs_name_str)),
(
lhs_expr.span.between(rhs_expr.span),
".wrapping_add(".to_owned(),
),
(rhs_expr.span.shrink_to_hi(), ")".to_owned()),
],
Applicability::MaybeIncorrect,
);
}
Op::AssignOp(Spanned { node: hir::AssignOpKind::SubAssign, .. }) => {
if lhs_ty.is_raw_ptr() && rhs_ty.is_integral() {
err.multipart_suggestion(
"consider using `sub` or `wrapping_sub` to do pointer arithmetic",
vec![
(lhs_expr.span.shrink_to_lo(), format!("{} = ", lhs_name_str)),
(
lhs_expr.span.between(rhs_expr.span),
".wrapping_sub(".to_owned(),

),
(rhs_expr.span.shrink_to_hi(), ")".to_owned()),
],
Applicability::MaybeIncorrect,
);
}
}
_ => {}
}
}

let reported = err.emit();
Ty::new_error(self.tcx, reported)
}
Expand Down
Loading
Loading