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 12 pull requests #89608

Merged
merged 38 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
18df8d6
Expand documentation for `FpCategory`.
kpreid Aug 31, 2021
25b6f9b
print-type-sizes: skip field printing for primitives
tmiasko Sep 28, 2021
6cc91cb
Rename `std::thread::available_onccurrency` to `std::thread::availabl…
yoshuawuyts Sep 28, 2021
fe11483
Add functions to add unsigned and signed integers
a1phyr Jul 29, 2021
ab9f8a0
Apply suggestion for `overflowing_add_signed`
a1phyr Jul 30, 2021
b5dd522
Fix doc test
a1phyr Aug 7, 2021
9faf621
Add methods to add/sub `uX` to/from `iX`
a1phyr Aug 7, 2021
70e55a8
Apply suggestions
a1phyr Oct 3, 2021
4846fd9
Revert suggested use of `unwrap_or`
a1phyr Oct 3, 2021
a8a40ea
librustdoc: Use correct heading levels.
Oct 1, 2021
03fbc16
Add doc aliases to `std::thread::available_parallelism`
yoshuawuyts Oct 4, 2021
47edde1
Optimize `saturating_add_signed`
a1phyr Oct 4, 2021
388bcc1
Fix suggestion to borrow when casting from pointer to reference
FabianWolff Oct 4, 2021
4a6aa6e
Fix heading for methods on trait impls.
Oct 5, 2021
6518a0a
Change `Markdown(...)` to `Markdown { ... }`
Oct 5, 2021
13558ee
No need to default offset since we always override it.
Oct 5, 2021
f1425c7
heading_level: u32 -> heading_offset: HeadingOffset
Oct 5, 2021
08a4f24
Add tests for ensuring docblock headings.
Oct 5, 2021
c79447e
library std, libc dependency update
devnexen Oct 4, 2021
b2e4e59
refactor: VecDeques Drain fields to private
DeveloperC286 Sep 17, 2021
5af61cb
refactor: VecDeques IterMut fields to private
DeveloperC286 Sep 25, 2021
3c974ad
Note specific regions involved in 'borrowed data escapes' error
Aaron1011 Oct 3, 2021
edfd6d5
test
BoxyUwU Oct 6, 2021
b386959
fix: alloc-optimisation is only for rust llvm
infinity0 Oct 6, 2021
742d8be
Restore h1 styles, which got accidentally removed.
Oct 6, 2021
1f86a8e
Revert the rustc_error_codes changes.
Oct 6, 2021
3209582
Rollup merge of #87601 - a1phyr:feature_uint_add_signed, r=kennytm
Manishearth Oct 6, 2021
1e3b5d6
Rollup merge of #88523 - kpreid:category, r=yaahc
Manishearth Oct 6, 2021
91e3b51
Rollup merge of #89050 - DeveloperC286:drain_fields_to_private, r=jos…
Manishearth Oct 6, 2021
14da7fc
Rollup merge of #89245 - DeveloperC286:iter_mut_fields_to_private, r=…
Manishearth Oct 6, 2021
b4615b5
Rollup merge of #89324 - yoshuawuyts:hardware-parallelism, r=m-ou-se
Manishearth Oct 6, 2021
b87a9a8
Rollup merge of #89329 - tmiasko:print-type-sizes-no-fields, r=jackh726
Manishearth Oct 6, 2021
b015940
Rollup merge of #89501 - Aaron1011:escaping-name-regions, r=davidtwco
Manishearth Oct 6, 2021
7d6feb4
Rollup merge of #89506 - yaymukund:docblock-headings, r=GuillaumeGomez
Manishearth Oct 6, 2021
f5bfa34
Rollup merge of #89528 - FabianWolff:issue-89497, r=jackh726
Manishearth Oct 6, 2021
79a1fc8
Rollup merge of #89531 - devnexen:stack_overflow_bsd_libc_upd, r=dtolnay
Manishearth Oct 6, 2021
eb7da35
Rollup merge of #89588 - BoxyUwU:add_a_test_uwu, r=lcnr
Manishearth Oct 6, 2021
f31c805
Rollup merge of #89591 - infinity0:master, r=Amanieu
Manishearth Oct 6, 2021
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
44 changes: 36 additions & 8 deletions compiler/rustc_typeck/src/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
);
let mut sugg = None;
let mut sugg_mutref = false;
if let ty::Ref(reg, _, mutbl) = *self.cast_ty.kind() {
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
if let ty::RawPtr(TypeAndMut { ty: expr_ty, .. }) = *self.expr_ty.kind() {
if fcx
.try_coerce(
Expand All @@ -366,7 +366,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
)
.is_ok()
{
sugg = Some(format!("&{}*", mutbl.prefix_str()));
sugg = Some((format!("&{}*", mutbl.prefix_str()), cast_ty == expr_ty));
}
} else if let ty::Ref(expr_reg, expr_ty, expr_mutbl) = *self.expr_ty.kind() {
if expr_mutbl == Mutability::Not
Expand Down Expand Up @@ -400,7 +400,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
)
.is_ok()
{
sugg = Some(format!("&{}", mutbl.prefix_str()));
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
}
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind() {
if fcx
Expand All @@ -416,19 +416,47 @@ impl<'a, 'tcx> CastCheck<'tcx> {
)
.is_ok()
{
sugg = Some(format!("&{}", mutbl.prefix_str()));
sugg = Some((format!("&{}", mutbl.prefix_str()), false));
}
}
if sugg_mutref {
err.span_label(self.span, "invalid cast");
err.span_note(self.expr.span, "this reference is immutable");
err.span_note(self.cast_span, "trying to cast to a mutable reference type");
} else if let Some(sugg) = sugg {
} else if let Some((sugg, remove_cast)) = sugg {
err.span_label(self.span, "invalid cast");
err.span_suggestion_verbose(
self.expr.span.shrink_to_lo(),

let has_parens = fcx
.tcx
.sess
.source_map()
.span_to_snippet(self.expr.span)
.map_or(false, |snip| snip.starts_with("("));

// Very crude check to see whether the expression must be wrapped
// in parentheses for the suggestion to work (issue #89497).
// Can/should be extended in the future.
let needs_parens = !has_parens
&& match self.expr.kind {
hir::ExprKind::Cast(..) => true,
_ => false,
};

let mut suggestion = vec![(self.expr.span.shrink_to_lo(), sugg)];
if needs_parens {
suggestion[0].1 += "(";
suggestion.push((self.expr.span.shrink_to_hi(), ")".to_string()));
}
if remove_cast {
suggestion.push((
self.expr.span.shrink_to_hi().to(self.cast_span),
String::new(),
));
}

err.multipart_suggestion_verbose(
"consider borrowing the value",
sugg,
suggestion,
Applicability::MachineApplicable,
);
} else if !matches!(
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/cast/issue-89497.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Regression test for issue #89497.

// run-rustfix

fn main() {
let pointer: usize = &1_i32 as *const i32 as usize;
let _reference: &'static i32 = unsafe { &*(pointer as *const i32) };
//~^ ERROR: non-primitive cast
//~| HELP: consider borrowing the value
}
10 changes: 10 additions & 0 deletions src/test/ui/cast/issue-89497.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Regression test for issue #89497.

// run-rustfix

fn main() {
let pointer: usize = &1_i32 as *const i32 as usize;
let _reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
//~^ ERROR: non-primitive cast
//~| HELP: consider borrowing the value
}
15 changes: 15 additions & 0 deletions src/test/ui/cast/issue-89497.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0605]: non-primitive cast: `*const i32` as `&'static i32`
--> $DIR/issue-89497.rs:7:45
|
LL | let _reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
|
help: consider borrowing the value
|
LL - let _reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
LL + let _reference: &'static i32 = unsafe { &*(pointer as *const i32) };
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0605`.
5 changes: 3 additions & 2 deletions src/test/ui/error-codes/E0605.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ LL | v as &u8;
|
help: consider borrowing the value
|
LL | &*v as &u8;
| ++
LL - v as &u8;
LL + &*v;
|

error: aborting due to 2 previous errors

Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/issues/issue-2995.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ LL | let _q: &isize = p as &isize;
|
help: consider borrowing the value
|
LL | let _q: &isize = &*p as &isize;
| ++
LL - let _q: &isize = p as &isize;
LL + let _q: &isize = &*p;
|

error: aborting due to previous error

Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/mismatched_types/cast-rfc0401.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ LL | let _ = v as &u8;
|
help: consider borrowing the value
|
LL | let _ = &*v as &u8;
| ++
LL - let _ = v as &u8;
LL + let _ = &*v;
|

error[E0605]: non-primitive cast: `*const u8` as `E`
--> $DIR/cast-rfc0401.rs:30:13
Expand Down