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 9 pull requests #98472

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
00da0e7
make const_err show up in future breakage reports
RalfJung Jun 4, 2022
4a4d877
bless remaining tests
RalfJung Jun 4, 2022
ab3a2a0
Unify copying data from enclave to userspace
raoulstrackx Mar 29, 2022
531752f
Mitigate MMIO stale data vulnerabilities
raoulstrackx Mar 28, 2022
6f7d193
Ensure userspace allocation is 8-byte aligned
raoulstrackx Mar 23, 2022
a27aace
Test `copy_to_userspace` function
raoulstrackx Mar 22, 2022
edb6c4b
Add a test for issue #33172
rylev May 16, 2022
f30c76a
Turn off cdb test for now, link to issue
rylev Jun 20, 2022
3ea686f
Turn CDB test back on and all clarifying test
rylev Jun 20, 2022
e5402e4
Fix linux tests
rylev Jun 21, 2022
1a25ac9
Add comment about issue caused with multiple statics
rylev Jun 21, 2022
6a6910e
Address reviewer comments
raoulstrackx Jun 22, 2022
d23eea5
Add tracking issues to `--extern` option docs.
ehuss Jun 22, 2022
cc4f804
Move help popup into a pocket menu as well
GuillaumeGomez Jun 20, 2022
3eb9e1a
Add/update GUI tests for help pocket menu
GuillaumeGomez Jun 20, 2022
e4b2b41
Merge all popover hide functions into one
GuillaumeGomez Jun 22, 2022
23d325e
Update FIXME comment
rylev Jun 23, 2022
3c7f1f1
Suggest defining variable as mutable on `&mut _` type mismatch in pats
WaffleLapkin Jun 23, 2022
4c4fb71
add test
b-naber Jun 23, 2022
2e3221a
use correct substs in enum discriminant hack
b-naber Jun 23, 2022
38814fc
small refactor
b-naber Jun 24, 2022
f39c0d6
address review
b-naber Jun 24, 2022
ada2acc
Set relocation_model to Pic on emscripten target
hoodmane Jun 15, 2022
bf48b62
fmt
b-naber Jun 24, 2022
e25129b
take advantage of a labelled block
WaffleLapkin Jun 24, 2022
1dfb53b
improve wording of a suggestion
WaffleLapkin Jun 24, 2022
c06d8f9
Fix trait object reborrow suggestion
compiler-errors Jun 20, 2022
25fe474
Note concrete type being coerced into object
compiler-errors Jun 20, 2022
459b151
Rollup merge of #97085 - rylev:test-issue-33172, r=wesleywiser
compiler-errors Jun 25, 2022
7cf4f09
Rollup merge of #97743 - RalfJung:const-err-future-breakage, r=estebank
compiler-errors Jun 25, 2022
11cb0bb
Rollup merge of #98126 - fortanix:raoul/mitigate_stale_data_vulnerabi…
compiler-errors Jun 25, 2022
0bfffb7
Rollup merge of #98149 - hoodmane:emscripten-pic, r=petrochenkov
compiler-errors Jun 25, 2022
be2ef3e
Rollup merge of #98277 - compiler-errors:issue-93596, r=estebank
compiler-errors Jun 25, 2022
1c2e6b6
Rollup merge of #98297 - GuillaumeGomez:help-pocket-menu, r=notriddle
compiler-errors Jun 25, 2022
a6f8881
Rollup merge of #98401 - ehuss:extern-tracking, r=Dylan-DPC
compiler-errors Jun 25, 2022
6030d20
Rollup merge of #98429 - b-naber:use-correct-substs-discriminant-cast…
compiler-errors Jun 25, 2022
bdd6a25
Rollup merge of #98431 - WaffleLapkin:mut_pat_suggestions, r=compiler…
compiler-errors Jun 25, 2022
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
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ declare_lint! {
"constant evaluation encountered erroneous expression",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #71800 <https://github.com/rust-lang/rust/issues/71800>",
reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow,
};
report_in_external_macro
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub enum ObligationCauseCode<'tcx> {
ObjectTypeBound(Ty<'tcx>, ty::Region<'tcx>),

/// Obligation incurred due to an object cast.
ObjectCastObligation(/* Object type */ Ty<'tcx>),
ObjectCastObligation(/* Concrete type */ Ty<'tcx>, /* Object type */ Ty<'tcx>),

/// Obligation incurred due to a coercion.
Coercion {
Expand Down
186 changes: 94 additions & 92 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::thir::cx::region::Scope;
use crate::thir::cx::Cx;
use crate::thir::util::UserAnnotatedTyHelpers;
use rustc_data_structures::stack::ensure_sufficient_stack;
Expand Down Expand Up @@ -158,6 +159,98 @@ impl<'tcx> Cx<'tcx> {
Expr { temp_lifetime, ty: adjustment.target, span, kind }
}

/// Lowers a cast expression.
///
/// Dealing with user type annotations is left to the caller.
fn mirror_expr_cast(
&mut self,
source: &'tcx hir::Expr<'tcx>,
temp_lifetime: Option<Scope>,
span: Span,
) -> ExprKind<'tcx> {
let tcx = self.tcx;

// Check to see if this cast is a "coercion cast", where the cast is actually done
// using a coercion (or is a no-op).
if self.typeck_results().is_coercion_cast(source.hir_id) {
// Convert the lexpr to a vexpr.
ExprKind::Use { source: self.mirror_expr(source) }
} else if self.typeck_results().expr_ty(source).is_region_ptr() {
// Special cased so that we can type check that the element
// type of the source matches the pointed to type of the
// destination.
ExprKind::Pointer {
source: self.mirror_expr(source),
cast: PointerCast::ArrayToPointer,
}
} else {
// check whether this is casting an enum variant discriminant
// to prevent cycles, we refer to the discriminant initializer
// which is always an integer and thus doesn't need to know the
// enum's layout (or its tag type) to compute it during const eval
// Example:
// enum Foo {
// A,
// B = A as isize + 4,
// }
// The correct solution would be to add symbolic computations to miri,
// so we wouldn't have to compute and store the actual value

let hir::ExprKind::Path(ref qpath) = source.kind else {
return ExprKind::Cast { source: self.mirror_expr(source)};
};

let res = self.typeck_results().qpath_res(qpath, source.hir_id);
let ty = self.typeck_results().node_type(source.hir_id);
let ty::Adt(adt_def, substs) = ty.kind() else {
return ExprKind::Cast { source: self.mirror_expr(source)};
};

let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res else {
return ExprKind::Cast { source: self.mirror_expr(source)};
};

let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
let (discr_did, discr_offset) = adt_def.discriminant_def_for_variant(idx);

use rustc_middle::ty::util::IntTypeExt;
let ty = adt_def.repr().discr_type();
let discr_ty = ty.to_ty(tcx);

let param_env_ty = self.param_env.and(discr_ty);
let size = tcx
.layout_of(param_env_ty)
.unwrap_or_else(|e| {
panic!("could not compute layout for {:?}: {:?}", param_env_ty, e)
})
.size;

let lit = ScalarInt::try_from_uint(discr_offset as u128, size).unwrap();
let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
let offset = self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });

let source = match discr_did {
// in case we are offsetting from a computed discriminant
// and not the beginning of discriminants (which is always `0`)
Some(did) => {
let kind = ExprKind::NamedConst { def_id: did, substs, user_ty: None };
let lhs =
self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });
let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
self.thir.exprs.push(Expr {
temp_lifetime,
ty: discr_ty,
span: span,
kind: bin,
})
}
None => offset,
};

ExprKind::Cast { source }
}
}

fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
let tcx = self.tcx;
let expr_ty = self.typeck_results().expr_ty(expr);
Expand Down Expand Up @@ -604,98 +697,7 @@ impl<'tcx> Cx<'tcx> {
expr, cast_ty.hir_id, user_ty,
);

// Check to see if this cast is a "coercion cast", where the cast is actually done
// using a coercion (or is a no-op).
let cast = if self.typeck_results().is_coercion_cast(source.hir_id) {
// Convert the lexpr to a vexpr.
ExprKind::Use { source: self.mirror_expr(source) }
} else if self.typeck_results().expr_ty(source).is_region_ptr() {
// Special cased so that we can type check that the element
// type of the source matches the pointed to type of the
// destination.
ExprKind::Pointer {
source: self.mirror_expr(source),
cast: PointerCast::ArrayToPointer,
}
} else {
// check whether this is casting an enum variant discriminant
// to prevent cycles, we refer to the discriminant initializer
// which is always an integer and thus doesn't need to know the
// enum's layout (or its tag type) to compute it during const eval
// Example:
// enum Foo {
// A,
// B = A as isize + 4,
// }
// The correct solution would be to add symbolic computations to miri,
// so we wouldn't have to compute and store the actual value
let var = if let hir::ExprKind::Path(ref qpath) = source.kind {
let res = self.typeck_results().qpath_res(qpath, source.hir_id);
self.typeck_results().node_type(source.hir_id).ty_adt_def().and_then(
|adt_def| match res {
Res::Def(
DefKind::Ctor(CtorOf::Variant, CtorKind::Const),
variant_ctor_id,
) => {
let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
let (d, o) = adt_def.discriminant_def_for_variant(idx);
use rustc_middle::ty::util::IntTypeExt;
let ty = adt_def.repr().discr_type();
let ty = ty.to_ty(tcx);
Some((d, o, ty))
}
_ => None,
},
)
} else {
None
};

let source = if let Some((did, offset, var_ty)) = var {
let param_env_ty = self.param_env.and(var_ty);
let size = tcx
.layout_of(param_env_ty)
.unwrap_or_else(|e| {
panic!("could not compute layout for {:?}: {:?}", param_env_ty, e)
})
.size;
let lit = ScalarInt::try_from_uint(offset as u128, size).unwrap();
let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
let offset = self.thir.exprs.push(Expr {
temp_lifetime,
ty: var_ty,
span: expr.span,
kind,
});
match did {
Some(did) => {
// in case we are offsetting from a computed discriminant
// and not the beginning of discriminants (which is always `0`)
let substs = InternalSubsts::identity_for_item(tcx, did);
let kind =
ExprKind::NamedConst { def_id: did, substs, user_ty: None };
let lhs = self.thir.exprs.push(Expr {
temp_lifetime,
ty: var_ty,
span: expr.span,
kind,
});
let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
self.thir.exprs.push(Expr {
temp_lifetime,
ty: var_ty,
span: expr.span,
kind: bin,
})
}
None => offset,
}
} else {
self.mirror_expr(source)
};

ExprKind::Cast { source: source }
};
let cast = self.mirror_expr_cast(*source, temp_lifetime, expr.span);

if let Some(user_ty) = user_ty {
// NOTE: Creating a new Expr and wrapping a Cast inside of it may be
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{cvs, wasm_base};
use super::{LinkArgs, LinkerFlavor, PanicStrategy, Target, TargetOptions};
use super::{LinkArgs, LinkerFlavor, PanicStrategy, RelocModel, Target, TargetOptions};

pub fn target() -> Target {
let mut options = wasm_base::options();
Expand All @@ -26,6 +26,7 @@ pub fn target() -> Target {
// functionality, and a .wasm file.
exe_suffix: ".js".into(),
linker: None,
relocation_model: RelocModel::Pic,
panic_strategy: PanicStrategy::Unwind,
no_default_libraries: false,
post_link_args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
err.span_label(span, explanation);
}

if let ObligationCauseCode::ObjectCastObligation(obj_ty) = obligation.cause.code().peel_derives() &&
let Some(self_ty) = trait_predicate.self_ty().no_bound_vars() &&
if let ObligationCauseCode::ObjectCastObligation(concrete_ty, obj_ty) = obligation.cause.code().peel_derives() &&
Some(trait_ref.def_id()) == self.tcx.lang_items().sized_trait() {
self.suggest_borrowing_for_object_cast(&mut err, &obligation, self_ty, *obj_ty);
self.suggest_borrowing_for_object_cast(&mut err, &root_obligation, *concrete_ty, *obj_ty);
}

if trait_predicate.is_const_if_const() && obligation.param_env.is_const() {
Expand Down Expand Up @@ -1560,7 +1559,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
obligation.cause.code().peel_derives(),
ObligationCauseCode::ItemObligation(_)
| ObligationCauseCode::BindingObligation(_, _)
| ObligationCauseCode::ObjectCastObligation(_)
| ObligationCauseCode::ObjectCastObligation(..)
| ObligationCauseCode::OpaqueType
);
if let Err(error) = self.at(&obligation.cause, obligation.param_env).eq_exp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2217,9 +2217,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
err.span_note(tcx.def_span(item_def_id), &descr);
}
}
ObligationCauseCode::ObjectCastObligation(object_ty) => {
ObligationCauseCode::ObjectCastObligation(concrete_ty, object_ty) => {
err.note(&format!(
"required for the cast to the object type `{}`",
"required for the cast from `{}` to the object type `{}`",
self.ty_to_string(concrete_ty),
self.ty_to_string(object_ty)
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let cause = ObligationCause::new(
obligation.cause.span,
obligation.cause.body_id,
ObjectCastObligation(target),
ObjectCastObligation(source, target),
);
let outlives = ty::OutlivesPredicate(r_a, r_b);
nested.push(Obligation::with_depth(
Expand Down Expand Up @@ -910,7 +910,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let cause = ObligationCause::new(
obligation.cause.span,
obligation.cause.body_id,
ObjectCastObligation(target),
ObjectCastObligation(source, target),
);
let outlives = ty::OutlivesPredicate(r_a, r_b);
nested.push(Obligation::with_depth(
Expand All @@ -931,7 +931,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let cause = ObligationCause::new(
obligation.cause.span,
obligation.cause.body_id,
ObjectCastObligation(target),
ObjectCastObligation(source, target),
);

let predicate_to_obligation = |predicate| {
Expand Down
51 changes: 51 additions & 0 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,46 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ast::Mutability::Not => "",
};

let mut_var_suggestion = 'block: {
if !matches!(mutbl, ast::Mutability::Mut) {
break 'block None;
}

let ident_kind = match binding_parent {
hir::Node::Param(_) => "parameter",
hir::Node::Local(_) => "variable",
hir::Node::Arm(_) => "binding",

// Provide diagnostics only if the parent pattern is struct-like,
// i.e. where `mut binding` makes sense
hir::Node::Pat(Pat { kind, .. }) => match kind {
PatKind::Struct(..)
| PatKind::TupleStruct(..)
| PatKind::Or(..)
| PatKind::Tuple(..)
| PatKind::Slice(..) => "binding",

PatKind::Wild
| PatKind::Binding(..)
| PatKind::Path(..)
| PatKind::Box(..)
| PatKind::Ref(..)
| PatKind::Lit(..)
| PatKind::Range(..) => break 'block None,
},

// Don't provide suggestions in other cases
_ => break 'block None,
};

Some((
pat.span,
format!("to declare a mutable {ident_kind} use"),
format!("mut {binding}"),
))

};

match binding_parent {
// Check that there is explicit type (ie this is not a closure param with inferred type)
// so we don't suggest moving something to the type that does not exist
Expand All @@ -675,6 +715,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
],
Applicability::MachineApplicable
);

if let Some((sp, msg, sugg)) = mut_var_suggestion {
err.span_note(sp, format!("{msg}: `{sugg}`"));
}
}
hir::Node::Param(_) | hir::Node::Arm(_) | hir::Node::Pat(_) => {
// rely on match ergonomics or it might be nested `&&pat`
Expand All @@ -684,6 +728,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"",
Applicability::MaybeIncorrect,
);

if let Some((sp, msg, sugg)) = mut_var_suggestion {
err.span_note(sp, format!("{msg}: `{sugg}`"));
}
}
_ if let Some((sp, msg, sugg)) = mut_var_suggestion => {
err.span_suggestion(sp, msg, sugg, Applicability::MachineApplicable);
}
_ => {} // don't provide suggestions in other cases #55175
}
Expand Down
Loading