Skip to content
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

Rollup of 8 pull requests #114421

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
864368a
Fix wrong span for trait selection failure error reporting
chenyukang Jul 22, 2023
1d45658
Some tracing changes
oli-obk Jul 24, 2023
d14569b
Simplify a shadowing replacement (that sometimes identity replaces) w…
oli-obk Jul 24, 2023
e390dc9
Perform OpaqueCast field projection on HIR, too.
oli-obk Jul 24, 2023
8e32dad
parser: more friendly hints for handling `async move` in the 2015 edi…
bvanjoi Jul 30, 2023
049c728
Suggests turbofish in patterns
mu001999 Aug 1, 2023
743ae5a
Expand incorrect_fn_null_check lint with reference null checking
Urgau Jul 12, 2023
d2b7c80
Rename incorrect_fn_null_checks to useless_ptr_null_checks
Urgau Jul 13, 2023
84c5372
Rename incorrect_fn_null_checks to useless_ptr_null_checks (clippy side)
Urgau Jul 13, 2023
ef3413d
Add more tests for useless_ptr_null_checks lint
Urgau Jul 13, 2023
0b9529c
Add diagnostic items for `<*const _>::cast` and `ptr::from_mut`
Urgau Jul 13, 2023
89b2fe7
Keep the suggestion for wrong arbitrary self types
mu001999 Aug 2, 2023
f5243d2
Fix rustfmt dep
mu001999 Aug 2, 2023
6ae2677
[rustc_span][perf] Hoist lookup sorted by words out of the loop.
ttsugriy Aug 3, 2023
41e85c3
Apply suggestions
mu001999 Aug 3, 2023
2195fa6
fix the span in the suggestion of remove question mark
bvanjoi Aug 3, 2023
4b3dadb
Also lint on cast/cast_mut and ptr::from_mut/ptr::from_ref
Urgau Jul 13, 2023
8c8af6c
Avoid too many expected symbols and reduce `None`s
mu001999 Aug 3, 2023
ee51953
Also add label with original type for function pointers
Urgau Aug 3, 2023
dce7e87
Reduce arbitrary self type suggestions
mu001999 Aug 3, 2023
fa9d5e7
match scrutinee need necessary parentheses for structs
chenyukang Jul 14, 2023
bf7ef1b
Rollup merge of #113657 - Urgau:expand-incorrect_fn_null_check-lint, …
matthiaskrgr Aug 3, 2023
c23347a
Rollup merge of #113679 - chenyukang:yukang-fix-lint-113459, r=cjgillot
matthiaskrgr Aug 3, 2023
208ea74
Rollup merge of #113945 - chenyukang:yukang-fix-113447-slice-2, r=cjg…
matthiaskrgr Aug 3, 2023
d667761
Rollup merge of #114022 - oli-obk:tait_ice_alias_field_projection, r=…
matthiaskrgr Aug 3, 2023
b735a65
Rollup merge of #114237 - bvanjoi:fix-114219, r=cjgillot
matthiaskrgr Aug 3, 2023
17e9111
Rollup merge of #114300 - MU001999:fix/turbofish-pat, r=estebank
matthiaskrgr Aug 3, 2023
2f559ce
Rollup merge of #114395 - ttsugriy:hoist-lookup, r=lqd
matthiaskrgr Aug 3, 2023
6ed8688
Rollup merge of #114403 - bvanjoi:fix-114392, r=estebank
matthiaskrgr Aug 3, 2023
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
6 changes: 2 additions & 4 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,13 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {

/// Checks that the types internal to the `place` match up with
/// what would be expected.
#[instrument(level = "debug", skip(self, location), ret)]
fn sanitize_place(
&mut self,
place: &Place<'tcx>,
location: Location,
context: PlaceContext,
) -> PlaceTy<'tcx> {
debug!("sanitize_place: {:?}", place);

let mut place_ty = PlaceTy::from_ty(self.body().local_decls[place.local].ty);

for elem in place.projection.iter() {
Expand Down Expand Up @@ -608,7 +607,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
}
}

#[instrument(skip(self), level = "debug")]
#[instrument(skip(self, location), ret, level = "debug")]
fn sanitize_projection(
&mut self,
base: PlaceTy<'tcx>,
Expand All @@ -617,7 +616,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
location: Location,
context: PlaceContext,
) -> PlaceTy<'tcx> {
debug!("sanitize_projection: {:?} {:?} {:?}", base, pi, place);
let tcx = self.tcx();
let base_ty = base.ty;
match pi {
Expand Down
84 changes: 40 additions & 44 deletions compiler/rustc_hir_typeck/src/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,14 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
}

/// Like `pat_ty`, but ignores implicit `&` patterns.
#[instrument(level = "debug", skip(self), ret)]
fn pat_ty_unadjusted(&self, pat: &hir::Pat<'_>) -> McResult<Ty<'tcx>> {
let base_ty = self.node_ty(pat.hir_id)?;
debug!("pat_ty(pat={:?}) base_ty={:?}", pat, base_ty);
trace!(?base_ty);

// This code detects whether we are looking at a `ref x`,
// and if so, figures out what the type *being borrowed* is.
let ret_ty = match pat.kind {
match pat.kind {
PatKind::Binding(..) => {
let bm = *self
.typeck_results
Expand All @@ -217,21 +218,18 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
// but what we want here is the type of the underlying value being borrowed.
// So peel off one-level, turning the &T into T.
match base_ty.builtin_deref(false) {
Some(t) => t.ty,
Some(t) => Ok(t.ty),
None => {
debug!("By-ref binding of non-derefable type {:?}", base_ty);
return Err(());
debug!("By-ref binding of non-derefable type");
Err(())
}
}
} else {
base_ty
Ok(base_ty)
}
}
_ => base_ty,
};
debug!("pat_ty(pat={:?}) ret_ty={:?}", pat, ret_ty);

Ok(ret_ty)
_ => Ok(base_ty),
}
}

pub(crate) fn cat_expr(&self, expr: &hir::Expr<'_>) -> McResult<PlaceWithHirId<'tcx>> {
Expand Down Expand Up @@ -299,13 +297,11 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
}
}

#[instrument(level = "debug", skip(self))]
#[instrument(level = "debug", skip(self), ret)]
pub(crate) fn cat_expr_unadjusted(
&self,
expr: &hir::Expr<'_>,
) -> McResult<PlaceWithHirId<'tcx>> {
debug!("cat_expr: id={} expr={:?}", expr.hir_id, expr);

let expr_ty = self.expr_ty(expr)?;
match expr.kind {
hir::ExprKind::Unary(hir::UnOp::Deref, ref e_base) => {
Expand All @@ -319,7 +315,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {

hir::ExprKind::Field(ref base, _) => {
let base = self.cat_expr(base)?;
debug!("cat_expr(cat_field): id={} expr={:?} base={:?}", expr.hir_id, expr, base);
debug!(?base);

let field_idx = self
.typeck_results
Expand Down Expand Up @@ -389,7 +385,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
}
}

#[instrument(level = "debug", skip(self, span))]
#[instrument(level = "debug", skip(self, span), ret)]
pub(crate) fn cat_res(
&self,
hir_id: hir::HirId,
Expand Down Expand Up @@ -430,6 +426,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
/// Note: the actual upvar access contains invisible derefs of closure
/// environment and upvar reference as appropriate. Only regionck cares
/// about these dereferences, so we let it compute them as needed.
#[instrument(level = "debug", skip(self), ret)]
fn cat_upvar(&self, hir_id: hir::HirId, var_id: hir::HirId) -> McResult<PlaceWithHirId<'tcx>> {
let closure_expr_def_id = self.body_owner;

Expand All @@ -439,41 +436,44 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
};
let var_ty = self.node_ty(var_id)?;

let ret = PlaceWithHirId::new(hir_id, var_ty, PlaceBase::Upvar(upvar_id), Vec::new());

debug!("cat_upvar ret={:?}", ret);
Ok(ret)
Ok(PlaceWithHirId::new(hir_id, var_ty, PlaceBase::Upvar(upvar_id), Vec::new()))
}

#[instrument(level = "debug", skip(self), ret)]
pub(crate) fn cat_rvalue(
&self,
hir_id: hir::HirId,
span: Span,
expr_ty: Ty<'tcx>,
) -> PlaceWithHirId<'tcx> {
debug!("cat_rvalue hir_id={:?}, expr_ty={:?}, span={:?}", hir_id, expr_ty, span);
let ret = PlaceWithHirId::new(hir_id, expr_ty, PlaceBase::Rvalue, Vec::new());
debug!("cat_rvalue ret={:?}", ret);
ret
PlaceWithHirId::new(hir_id, expr_ty, PlaceBase::Rvalue, Vec::new())
}

#[instrument(level = "debug", skip(self, node), ret)]
pub(crate) fn cat_projection<N: HirNode>(
&self,
node: &N,
base_place: PlaceWithHirId<'tcx>,
ty: Ty<'tcx>,
kind: ProjectionKind,
) -> PlaceWithHirId<'tcx> {
let place_ty = base_place.place.ty();
let mut projections = base_place.place.projections;

let node_ty = self.typeck_results.node_type(node.hir_id());
// Opaque types can't have field projections, but we can instead convert
// the current place in-place (heh) to the hidden type, and then apply all
// follow up projections on that.
if node_ty != place_ty && place_ty.has_opaque_types() {
projections.push(Projection { kind: ProjectionKind::OpaqueCast, ty: node_ty });
}
projections.push(Projection { kind, ty });
let ret = PlaceWithHirId::new(
PlaceWithHirId::new(
node.hir_id(),
base_place.place.base_ty,
base_place.place.base,
projections,
);
debug!("cat_field ret {:?}", ret);
ret
)
}

#[instrument(level = "debug", skip(self))]
Expand All @@ -497,7 +497,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
self.cat_deref(expr, base)
}

#[instrument(level = "debug", skip(self, node))]
#[instrument(level = "debug", skip(self, node), ret)]
fn cat_deref(
&self,
node: &impl HirNode,
Expand All @@ -514,14 +514,12 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
let mut projections = base_place.place.projections;
projections.push(Projection { kind: ProjectionKind::Deref, ty: deref_ty });

let ret = PlaceWithHirId::new(
Ok(PlaceWithHirId::new(
node.hir_id(),
base_place.place.base_ty,
base_place.place.base,
projections,
);
debug!("cat_deref ret {:?}", ret);
Ok(ret)
))
}

pub(crate) fn cat_pattern<F>(
Expand Down Expand Up @@ -603,6 +601,13 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
}
}

/// Here, `place` is the `PlaceWithHirId` being matched and pat is the pattern it
/// is being matched against.
///
/// In general, the way that this works is that we walk down the pattern,
/// constructing a `PlaceWithHirId` that represents the path that will be taken
/// to reach the value being matched.
#[instrument(skip(self, op), ret, level = "debug")]
fn cat_pattern_<F>(
&self,
mut place_with_id: PlaceWithHirId<'tcx>,
Expand All @@ -612,15 +617,6 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
where
F: FnMut(&PlaceWithHirId<'tcx>, &hir::Pat<'_>),
{
// Here, `place` is the `PlaceWithHirId` being matched and pat is the pattern it
// is being matched against.
//
// In general, the way that this works is that we walk down the pattern,
// constructing a `PlaceWithHirId` that represents the path that will be taken
// to reach the value being matched.

debug!("cat_pattern(pat={:?}, place_with_id={:?})", pat, place_with_id);

// If (pattern) adjustments are active for this pattern, adjust the `PlaceWithHirId` correspondingly.
// `PlaceWithHirId`s are constructed differently from patterns. For example, in
//
Expand Down Expand Up @@ -654,11 +650,11 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
// `deref { deref { place_foo }}` instead of `place_foo` since the pattern is now `Some(x,)`
// and not `&&Some(x,)`, even though its assigned type is that of `&&Some(x,)`.
for _ in 0..self.typeck_results.pat_adjustments().get(pat.hir_id).map_or(0, |v| v.len()) {
debug!("cat_pattern: applying adjustment to place_with_id={:?}", place_with_id);
debug!("applying adjustment to place_with_id={:?}", place_with_id);
place_with_id = self.cat_deref(pat, place_with_id)?;
}
let place_with_id = place_with_id; // lose mutability
debug!("cat_pattern: applied adjustment derefs to get place_with_id={:?}", place_with_id);
debug!("applied adjustment derefs to get place_with_id={:?}", place_with_id);

// Invoke the callback, but only now, after the `place_with_id` has adjusted.
//
Expand Down
27 changes: 13 additions & 14 deletions compiler/rustc_hir_typeck/src/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.demand_eqtype(span, closure_kind.to_ty(self.tcx), closure_kind_ty);

// If we have an origin, store it.
if let Some(origin) = origin {
let origin = if enable_precise_capture(span) {
(origin.0, origin.1)
} else {
(origin.0, Place { projections: vec![], ..origin.1 })
};
if let Some(mut origin) = origin {
if !enable_precise_capture(span) {
// Without precise captures, we just capture the base and ignore
// the projections.
origin.1.projections.clear()
}

self.typeck_results
.borrow_mut()
Expand All @@ -294,10 +294,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Equate the type variables for the upvars with the actual types.
let final_upvar_tys = self.final_upvar_tys(closure_def_id);
debug!(
"analyze_closure: id={:?} args={:?} final_upvar_tys={:?}",
closure_hir_id, args, final_upvar_tys
);
debug!(?closure_hir_id, ?args, ?final_upvar_tys);

// Build a tuple (U0..Un) of the final upvar types U0..Un
// and unify the upvar tuple type in the closure with it:
Expand Down Expand Up @@ -338,10 +335,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let upvar_ty = captured_place.place.ty();
let capture = captured_place.info.capture_kind;

debug!(
"final_upvar_tys: place={:?} upvar_ty={:?} capture={:?}, mutability={:?}",
captured_place.place, upvar_ty, capture, captured_place.mutability,
);
debug!(?captured_place.place, ?upvar_ty, ?capture, ?captured_place.mutability);

apply_capture_kind_on_capture_ty(self.tcx, upvar_ty, capture, captured_place.region)
})
Expand Down Expand Up @@ -679,6 +673,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match (p1.kind, p2.kind) {
// Paths are the same, continue to next loop.
(ProjectionKind::Deref, ProjectionKind::Deref) => {}
(ProjectionKind::OpaqueCast, ProjectionKind::OpaqueCast) => {}
(ProjectionKind::Field(i1, _), ProjectionKind::Field(i2, _))
if i1 == i2 => {}

Expand All @@ -701,10 +696,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
l @ (ProjectionKind::Index
| ProjectionKind::Subslice
| ProjectionKind::Deref
| ProjectionKind::OpaqueCast
| ProjectionKind::Field(..)),
r @ (ProjectionKind::Index
| ProjectionKind::Subslice
| ProjectionKind::Deref
| ProjectionKind::OpaqueCast
| ProjectionKind::Field(..)),
) => bug!(
"ProjectionKinds Index or Subslice were unexpected: ({:?}, {:?})",
Expand Down Expand Up @@ -1890,6 +1887,7 @@ fn restrict_capture_precision(
return (place, curr_mode);
}
ProjectionKind::Deref => {}
ProjectionKind::OpaqueCast => {}
ProjectionKind::Field(..) => {} // ignore
}
}
Expand Down Expand Up @@ -1946,6 +1944,7 @@ fn construct_place_string<'tcx>(tcx: TyCtxt<'_>, place: &Place<'tcx>) -> String
ProjectionKind::Deref => String::from("Deref"),
ProjectionKind::Index => String::from("Index"),
ProjectionKind::Subslice => String::from("Subslice"),
ProjectionKind::OpaqueCast => String::from("OpaqueCast"),
};
if i != 0 {
projections_str.push(',');
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 @@ -764,7 +764,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
Some(ty) if expected == ty => {
let source_map = self.tcx.sess.source_map();
err.span_suggestion(
source_map.end_point(cause.span),
source_map.end_point(cause.span()),
"try removing this `?`",
"",
Applicability::MachineApplicable,
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ lint_expectation = this lint expectation is unfulfilled
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
.rationale = {$rationale}

lint_fn_null_check = function pointers are not nullable, so checking them for null will always return false
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value

lint_for_loops_over_fallibles =
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
.suggestion = consider using `if let` to clear intent
Expand Down Expand Up @@ -454,6 +451,13 @@ lint_path_statement_drop = path statement drops value

lint_path_statement_no_effect = path statement with no effect

lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
.label = expression has type `{$orig_ty}`

lint_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
.label = expression has type `{$orig_ty}`

lint_query_instability = using `{$query}` can result in unstable query results
.note = if you believe this case to be fine, allow this lint and add a comment explaining your rationale

Expand Down
Loading