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 7 pull requests #129060

Merged
merged 21 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
df27dfa
Optimize integer pow by removing exit branch
mzabaluev Mar 22, 2024
1faa101
Explicitly unroll integer pow for small exponents
mzabaluev Jul 11, 2024
2f23534
Use is_val_statically_known to optimize pow
mzabaluev Jul 12, 2024
1e445f4
Add must_use attribute to Coroutine trait
henryksloan Aug 13, 2024
ac88b33
Revert to original loop for const pow exponents
mzabaluev Aug 13, 2024
399ef23
Allow to customize `// TODO:` comment for deprecated safe autofix
tbu- Jul 17, 2024
811d7dd
`#[deprecated_safe_2024]`: Also use the `// TODO:` hint in the compil…
tbu- Jul 29, 2024
2a000c8
Don't panic on unknown JSON-like output lines
Zalathar Aug 13, 2024
355f264
Remove a confusing comment
Zalathar Aug 13, 2024
53e87d2
Remove duplicated rustdoc ui test
GuillaumeGomez Aug 13, 2024
d2177d9
Emit a warning instead of an error if `--generate-link-to-definition`…
GuillaumeGomez Aug 13, 2024
afbab80
Update rustdoc-ui test for `--generate-link-to-definition` option
GuillaumeGomez Aug 13, 2024
87a4c32
Fix target triple in bootstrap
Kobzol Aug 13, 2024
5082e25
Add mw back to review rotation
michaelwoerister Aug 13, 2024
bc9c31d
Rollup merge of #122884 - mzabaluev:pow-remove-exit-branch, r=Amanieu
matthiaskrgr Aug 13, 2024
f68a28d
Rollup merge of #127857 - tbu-:pr_deprecated_safe_todo, r=petrochenkov
matthiaskrgr Aug 13, 2024
d4f5a89
Rollup merge of #129034 - henryksloan:coroutine-must-use, r=joboet
matthiaskrgr Aug 13, 2024
65054ed
Rollup merge of #129049 - Zalathar:json-like, r=jieyouxu
matthiaskrgr Aug 13, 2024
5ef33d4
Rollup merge of #129050 - GuillaumeGomez:generate-link-to-definition-…
matthiaskrgr Aug 13, 2024
c013e2c
Rollup merge of #129056 - Kobzol:fix-target-triple, r=onur-ozkan
matthiaskrgr Aug 13, 2024
24a46c5
Rollup merge of #129058 - michaelwoerister:mwback082024, r=jackh726
matthiaskrgr Aug 13, 2024
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
4 changes: 2 additions & 2 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
through unstable paths"
),
rustc_attr!(
rustc_deprecated_safe_2024, Normal, template!(Word), WarnFollowing,
EncodeCrossCrate::Yes,
rustc_deprecated_safe_2024, Normal, template!(List: r#"audit_that = "...""#),
ErrorFollowing, EncodeCrossCrate::Yes,
"rustc_deprecated_safe_2024 is supposed to be used in libstd only",
),

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mir_build_call_to_deprecated_safe_fn_requires_unsafe =
call to deprecated safe function `{$function}` is unsafe and requires unsafe block
.note = consult the function's documentation for information on how to avoid undefined behavior
.label = call to unsafe function
.suggestion = you can wrap the call in an `unsafe` block if you can guarantee the code is only ever called from single-threaded code
.suggestion = you can wrap the call in an `unsafe` block if you can guarantee {$guarantee}

mir_build_call_to_fn_with_requires_unsafe =
call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block
Expand Down
28 changes: 26 additions & 2 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,42 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
// from an edition before 2024.
&UnsafeOpKind::CallToUnsafeFunction(Some(id))
if !span.at_least_rust_2024()
&& self.tcx.has_attr(id, sym::rustc_deprecated_safe_2024) =>
&& let Some(attr) = self.tcx.get_attr(id, sym::rustc_deprecated_safe_2024) =>
{
let suggestion = attr
.meta_item_list()
.unwrap_or_default()
.into_iter()
.find(|item| item.has_name(sym::audit_that))
.map(|item| {
item.value_str().expect(
"`#[rustc_deprecated_safe_2024(audit_that)]` must have a string value",
)
});

let sm = self.tcx.sess.source_map();
let guarantee = suggestion
.as_ref()
.map(|suggestion| format!("that {}", suggestion))
.unwrap_or_else(|| String::from("its unsafe preconditions"));
let suggestion = suggestion
.and_then(|suggestion| {
sm.indentation_before(span).map(|indent| {
format!("{}// TODO: Audit that {}.\n", indent, suggestion) // ignore-tidy-todo
})
})
.unwrap_or_default();

self.tcx.emit_node_span_lint(
DEPRECATED_SAFE_2024,
self.hir_context,
span,
CallToDeprecatedSafeFnRequiresUnsafe {
span,
function: with_no_trimmed_paths!(self.tcx.def_path_str(id)),
guarantee,
sub: CallToDeprecatedSafeFnRequiresUnsafeSub {
indent: sm.indentation_before(span).unwrap_or_default(),
start_of_line_suggestion: suggestion,
start_of_line: sm.span_extend_to_line(span).shrink_to_lo(),
left: span.shrink_to_lo(),
right: span.shrink_to_hi(),
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafe {
#[label]
pub(crate) span: Span,
pub(crate) function: String,
pub(crate) guarantee: String,
#[subdiagnostic]
pub(crate) sub: CallToDeprecatedSafeFnRequiresUnsafeSub,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(mir_build_suggestion, applicability = "machine-applicable")]
pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafeSub {
pub(crate) indent: String,
#[suggestion_part(
code = "{indent}// TODO: Audit that the environment access only happens in single-threaded code.\n" // ignore-tidy-todo
)]
pub(crate) start_of_line_suggestion: String,
#[suggestion_part(code = "{start_of_line_suggestion}")]
pub(crate) start_of_line: Span,
#[suggestion_part(code = "unsafe {{ ")]
pub(crate) left: Span,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ symbols! {
attr,
attr_literals,
attributes,
audit_that,
augmented_assignments,
auto_traits,
automatically_derived,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
#![feature(internal_impls_macro)]
#![feature(ip)]
#![feature(is_ascii_octdigit)]
#![feature(is_val_statically_known)]
#![feature(isqrt)]
#![feature(link_cfg)]
#![feature(offset_of_enum)]
Expand Down
118 changes: 75 additions & 43 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1496,18 +1496,17 @@ macro_rules! int_impl {
let mut base = self;
let mut acc: Self = 1;

while exp > 1 {
loop {
if (exp & 1) == 1 {
acc = try_opt!(acc.checked_mul(base));
// since exp!=0, finally the exp must be 1.
if exp == 1 {
return Some(acc);
}
}
exp /= 2;
base = try_opt!(base.checked_mul(base));
}
// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
acc.checked_mul(base)
}

/// Strict exponentiation. Computes `self.pow(exp)`, panicking if
Expand Down Expand Up @@ -1547,18 +1546,17 @@ macro_rules! int_impl {
let mut base = self;
let mut acc: Self = 1;

while exp > 1 {
loop {
if (exp & 1) == 1 {
acc = acc.strict_mul(base);
// since exp!=0, finally the exp must be 1.
if exp == 1 {
return acc;
}
}
exp /= 2;
base = base.strict_mul(base);
}
// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
acc.strict_mul(base)
}

/// Returns the square root of the number, rounded down.
Expand Down Expand Up @@ -2175,26 +2173,44 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
#[rustc_allow_const_fn_unstable(is_val_statically_known)]
pub const fn wrapping_pow(self, mut exp: u32) -> Self {
if exp == 0 {
return 1;
}
let mut base = self;
let mut acc: Self = 1;

while exp > 1 {
if (exp & 1) == 1 {
acc = acc.wrapping_mul(base);
if intrinsics::is_val_statically_known(exp) {
while exp > 1 {
if (exp & 1) == 1 {
acc = acc.wrapping_mul(base);
}
exp /= 2;
base = base.wrapping_mul(base);
}
exp /= 2;
base = base.wrapping_mul(base);
}

// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
acc.wrapping_mul(base)
// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary.
acc.wrapping_mul(base)
} else {
// This is faster than the above when the exponent is not known
// at compile time. We can't use the same code for the constant
// exponent case because LLVM is currently unable to unroll
// this loop.
loop {
if (exp & 1) == 1 {
acc = acc.wrapping_mul(base);
// since exp!=0, finally the exp must be 1.
if exp == 1 {
return acc;
}
}
exp /= 2;
base = base.wrapping_mul(base);
}
}
}

/// Calculates `self` + `rhs`.
Expand Down Expand Up @@ -2690,9 +2706,14 @@ macro_rules! int_impl {
// Scratch space for storing results of overflowing_mul.
let mut r;

while exp > 1 {
loop {
if (exp & 1) == 1 {
r = acc.overflowing_mul(base);
// since exp!=0, finally the exp must be 1.
if exp == 1 {
r.1 |= overflown;
return r;
}
acc = r.0;
overflown |= r.1;
}
Expand All @@ -2701,14 +2722,6 @@ macro_rules! int_impl {
base = r.0;
overflown |= r.1;
}

// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
r = acc.overflowing_mul(base);
r.1 |= overflown;
r
}

/// Raises self to the power of `exp`, using exponentiation by squaring.
Expand All @@ -2728,26 +2741,45 @@ macro_rules! int_impl {
without modifying the original"]
#[inline]
#[rustc_inherit_overflow_checks]
#[rustc_allow_const_fn_unstable(is_val_statically_known)]
pub const fn pow(self, mut exp: u32) -> Self {
if exp == 0 {
return 1;
}
let mut base = self;
let mut acc = 1;

while exp > 1 {
if (exp & 1) == 1 {
acc = acc * base;
if intrinsics::is_val_statically_known(exp) {
while exp > 1 {
if (exp & 1) == 1 {
acc = acc * base;
}
exp /= 2;
base = base * base;
}
exp /= 2;
base = base * base;
}

// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
acc * base
// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
acc * base
} else {
// This is faster than the above when the exponent is not known
// at compile time. We can't use the same code for the constant
// exponent case because LLVM is currently unable to unroll
// this loop.
loop {
if (exp & 1) == 1 {
acc = acc * base;
// since exp!=0, finally the exp must be 1.
if exp == 1 {
return acc;
}
}
exp /= 2;
base = base * base;
}
}
}

/// Returns the square root of the number, rounded down.
Expand Down
Loading
Loading