From 8035796b9aabf1aa2a711694e40f00fe8a822f40 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sat, 26 Mar 2022 18:23:32 -0400 Subject: [PATCH 1/7] Stablize `const_extern_fn` for "Rust" and "C" All other ABIs are left unstable for now. cc #64926 --- compiler/rustc_ast_passes/src/feature_gate.rs | 37 +++++++------ compiler/rustc_parse/src/parser/ty.rs | 3 ++ .../feature-gate-const_extern_fn.rs | 17 +++--- .../feature-gate-const_extern_fn.stderr | 54 ++++--------------- 4 files changed, 42 insertions(+), 69 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 5b6147c7223..5b2716b2dd0 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -58,9 +58,22 @@ struct PostExpansionVisitor<'a> { } impl<'a> PostExpansionVisitor<'a> { - fn check_abi(&self, abi: ast::StrLit) { + fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) { let ast::StrLit { symbol_unescaped, span, .. } = abi; + if let ast::Const::Yes(_) = constness { + match symbol_unescaped.as_str() { + // Stable + "Rust" | "C" => {} + abi => gate_feature_post!( + &self, + const_extern_fn, + span, + &format!("`{}` as a `const fn` ABI is unstable", abi) + ), + } + } + match symbol_unescaped.as_str() { // Stable "Rust" | "C" | "cdecl" | "stdcall" | "fastcall" | "aapcs" | "win64" | "sysv64" @@ -261,9 +274,9 @@ impl<'a> PostExpansionVisitor<'a> { } } - fn check_extern(&self, ext: ast::Extern) { + fn check_extern(&self, ext: ast::Extern, constness: ast::Const) { if let ast::Extern::Explicit(abi) = ext { - self.check_abi(abi); + self.check_abi(abi, constness); } } @@ -445,7 +458,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { match i.kind { ast::ItemKind::ForeignMod(ref foreign_module) => { if let Some(abi) = foreign_module.abi { - self.check_abi(abi); + self.check_abi(abi, ast::Const::No); } } @@ -568,7 +581,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_ty(&mut self, ty: &'a ast::Ty) { match ty.kind { ast::TyKind::BareFn(ref bare_fn_ty) => { - self.check_extern(bare_fn_ty.ext); + // Function pointers cannot be `const` + self.check_extern(bare_fn_ty.ext, ast::Const::No); } ast::TyKind::Never => { gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental"); @@ -668,18 +682,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) { if let Some(header) = fn_kind.header() { // Stability of const fn methods are covered in `visit_assoc_item` below. - self.check_extern(header.ext); - - if let (ast::Const::Yes(_), ast::Extern::Implicit) - | (ast::Const::Yes(_), ast::Extern::Explicit(_)) = (header.constness, header.ext) - { - gate_feature_post!( - &self, - const_extern_fn, - span, - "`const extern fn` definitions are unstable" - ); - } + self.check_extern(header.ext, header.constness); } if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() { diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 436c5bd4fca..bb387064e27 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -523,6 +523,9 @@ impl<'a> Parser<'a> { let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?; let whole_span = lo.to(self.prev_token.span); if let ast::Const::Yes(span) = constness { + // If we ever start to allow `const fn()`, then update + // feature gating for `#![feature(const_extern_fn)]` to + // cover it. self.error_fn_ptr_bad_qualifier(whole_span, span, "const"); } if let ast::Async::Yes { span, .. } = asyncness { diff --git a/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.rs b/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.rs index 5667d553527..f7bed91b037 100644 --- a/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.rs +++ b/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.rs @@ -1,10 +1,13 @@ -// Check that `const extern fn` and `const unsafe extern fn` are feature-gated. +// Check that `const extern fn` and `const unsafe extern fn` are feature-gated +// for certain ABIs. -const extern fn foo1() {} //~ ERROR `const extern fn` definitions are unstable -const extern "C" fn foo2() {} //~ ERROR `const extern fn` definitions are unstable -const extern "Rust" fn foo3() {} //~ ERROR `const extern fn` definitions are unstable -const unsafe extern fn bar1() {} //~ ERROR `const extern fn` definitions are unstable -const unsafe extern "C" fn bar2() {} //~ ERROR `const extern fn` definitions are unstable -const unsafe extern "Rust" fn bar3() {} //~ ERROR `const extern fn` definitions are unstable +const extern fn foo1() {} +const extern "C" fn foo2() {} +const extern "Rust" fn foo3() {} +const extern "cdecl" fn foo4() {} //~ ERROR `cdecl` as a `const fn` ABI is unstable +const unsafe extern fn bar1() {} +const unsafe extern "C" fn bar2() {} +const unsafe extern "Rust" fn bar3() {} +const unsafe extern "cdecl" fn bar4() {} //~ ERROR `cdecl` as a `const fn` ABI is unstable fn main() {} diff --git a/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.stderr b/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.stderr index bd5940a3fd6..f8c3107bd22 100644 --- a/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.stderr +++ b/src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.stderr @@ -1,57 +1,21 @@ -error[E0658]: `const extern fn` definitions are unstable - --> $DIR/feature-gate-const_extern_fn.rs:3:1 +error[E0658]: `cdecl` as a `const fn` ABI is unstable + --> $DIR/feature-gate-const_extern_fn.rs:7:14 | -LL | const extern fn foo1() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | const extern "cdecl" fn foo4() {} + | ^^^^^^^ | = note: see issue #64926 for more information = help: add `#![feature(const_extern_fn)]` to the crate attributes to enable -error[E0658]: `const extern fn` definitions are unstable - --> $DIR/feature-gate-const_extern_fn.rs:4:1 +error[E0658]: `cdecl` as a `const fn` ABI is unstable + --> $DIR/feature-gate-const_extern_fn.rs:11:21 | -LL | const extern "C" fn foo2() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | const unsafe extern "cdecl" fn bar4() {} + | ^^^^^^^ | = note: see issue #64926 for more information = help: add `#![feature(const_extern_fn)]` to the crate attributes to enable -error[E0658]: `const extern fn` definitions are unstable - --> $DIR/feature-gate-const_extern_fn.rs:5:1 - | -LL | const extern "Rust" fn foo3() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #64926 for more information - = help: add `#![feature(const_extern_fn)]` to the crate attributes to enable - -error[E0658]: `const extern fn` definitions are unstable - --> $DIR/feature-gate-const_extern_fn.rs:6:1 - | -LL | const unsafe extern fn bar1() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #64926 for more information - = help: add `#![feature(const_extern_fn)]` to the crate attributes to enable - -error[E0658]: `const extern fn` definitions are unstable - --> $DIR/feature-gate-const_extern_fn.rs:7:1 - | -LL | const unsafe extern "C" fn bar2() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #64926 for more information - = help: add `#![feature(const_extern_fn)]` to the crate attributes to enable - -error[E0658]: `const extern fn` definitions are unstable - --> $DIR/feature-gate-const_extern_fn.rs:8:1 - | -LL | const unsafe extern "Rust" fn bar3() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #64926 for more information - = help: add `#![feature(const_extern_fn)]` to the crate attributes to enable - -error: aborting due to 6 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0658`. From 82d99ad8548a7c6eb9de059554897865d3de96d2 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 15 Apr 2022 22:41:34 -0500 Subject: [PATCH 2/7] Make the debug output for `TargetSelection` less verbose In particular, this makes the output of `x build -vv` easier to read. Before: ``` c Sysroot { compiler: Compiler { stage: 0, host: TargetSelection { triple: "x86_64-unknown-linux-gnu", file: None } } } ``` After: ``` c Sysroot { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu } } ``` --- src/bootstrap/config.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index 9534cc5f434..d7c29f6900a 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -221,7 +221,7 @@ impl FromStr for LlvmLibunwind { } } -#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct TargetSelection { pub triple: Interned, file: Option>, @@ -276,6 +276,12 @@ impl fmt::Display for TargetSelection { } } +impl fmt::Debug for TargetSelection { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} + impl PartialEq<&str> for TargetSelection { fn eq(&self, other: &&str) -> bool { self.triple == *other From 02d12bc30c173fd0cbf55d67c42d6cbae231203a Mon Sep 17 00:00:00 2001 From: niluxv Date: Sat, 16 Apr 2022 10:05:33 +0200 Subject: [PATCH 3/7] Improve `lossy_provenance_casts` lint diagnostics Use `multipart_suggestion` and don't suggested unnecessary parenthesis. --- compiler/rustc_typeck/src/check/cast.rs | 37 +++++++++++++------ .../lint-strict-provenance-lossy-casts.rs | 7 ++++ .../lint-strict-provenance-lossy-casts.stderr | 34 +++++++++++++++-- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_typeck/src/check/cast.rs b/compiler/rustc_typeck/src/check/cast.rs index 049940d19a6..da054077376 100644 --- a/compiler/rustc_typeck/src/check/cast.rs +++ b/compiler/rustc_typeck/src/check/cast.rs @@ -993,20 +993,33 @@ impl<'a, 'tcx> CastCheck<'tcx> { )); let msg = "use `.addr()` to obtain the address of a pointer"; - if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) { - let scalar_cast = match t_c { - ty::cast::IntTy::U(ty::UintTy::Usize) => String::new(), - _ => format!(" as {}", self.cast_ty), - }; - err.span_suggestion( - self.span, - msg, - format!("({snippet}).addr(){scalar_cast}"), - Applicability::MaybeIncorrect - ); + + let expr_prec = self.expr.precedence().order(); + let needs_parens = expr_prec < rustc_ast::util::parser::PREC_POSTFIX; + + let scalar_cast = match t_c { + ty::cast::IntTy::U(ty::UintTy::Usize) => String::new(), + _ => format!(" as {}", self.cast_ty), + }; + + let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span); + + if needs_parens { + let suggestions = vec![ + (self.expr_span.shrink_to_lo(), String::from("(")), + (cast_span, format!(").addr(){scalar_cast}")), + ]; + + err.multipart_suggestion(msg, suggestions, Applicability::MaybeIncorrect); } else { - err.help(msg); + err.span_suggestion( + cast_span, + msg, + format!(".addr(){scalar_cast}"), + Applicability::MaybeIncorrect, + ); } + err.help( "if you can't comply with strict provenance and need to expose the pointer \ provenance you can use `.expose_addr()` instead" diff --git a/src/test/ui/lint/lint-strict-provenance-lossy-casts.rs b/src/test/ui/lint/lint-strict-provenance-lossy-casts.rs index 3690fbc904d..9799a053756 100644 --- a/src/test/ui/lint/lint-strict-provenance-lossy-casts.rs +++ b/src/test/ui/lint/lint-strict-provenance-lossy-casts.rs @@ -8,4 +8,11 @@ fn main() { let addr_32bit = &x as *const u8 as u32; //~^ ERROR under strict provenance it is considered bad style to cast pointer `*const u8` to integer `u32` + + // don't add unnecessary parens in the suggestion + let ptr = &x as *const u8; + let ptr_addr = ptr as usize; + //~^ ERROR under strict provenance it is considered bad style to cast pointer `*const u8` to integer `usize` + let ptr_addr_32bit = ptr as u32; + //~^ ERROR under strict provenance it is considered bad style to cast pointer `*const u8` to integer `u32` } diff --git a/src/test/ui/lint/lint-strict-provenance-lossy-casts.stderr b/src/test/ui/lint/lint-strict-provenance-lossy-casts.stderr index e7a6c1837bd..05178b34b11 100644 --- a/src/test/ui/lint/lint-strict-provenance-lossy-casts.stderr +++ b/src/test/ui/lint/lint-strict-provenance-lossy-casts.stderr @@ -2,7 +2,7 @@ error: under strict provenance it is considered bad style to cast pointer `*cons --> $DIR/lint-strict-provenance-lossy-casts.rs:6:23 | LL | let addr: usize = &x as *const u8 as usize; - | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.addr()` to obtain the address of a pointer: `(&x as *const u8).addr()` + | ^^^^^^^^^^^^^^^^^^^^^^^^ | note: the lint level is defined here --> $DIR/lint-strict-provenance-lossy-casts.rs:2:9 @@ -10,14 +10,42 @@ note: the lint level is defined here LL | #![deny(lossy_provenance_casts)] | ^^^^^^^^^^^^^^^^^^^^^^ = help: if you can't comply with strict provenance and need to expose the pointer provenance you can use `.expose_addr()` instead +help: use `.addr()` to obtain the address of a pointer + | +LL | let addr: usize = (&x as *const u8).addr(); + | + ~~~~~~~~ error: under strict provenance it is considered bad style to cast pointer `*const u8` to integer `u32` --> $DIR/lint-strict-provenance-lossy-casts.rs:9:22 | LL | let addr_32bit = &x as *const u8 as u32; - | ^^^^^^^^^^^^^^^^^^^^^^ help: use `.addr()` to obtain the address of a pointer: `(&x as *const u8).addr() as u32` + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = help: if you can't comply with strict provenance and need to expose the pointer provenance you can use `.expose_addr()` instead +help: use `.addr()` to obtain the address of a pointer + | +LL | let addr_32bit = (&x as *const u8).addr() as u32; + | + ~~~~~~~~~~~~~~~ + +error: under strict provenance it is considered bad style to cast pointer `*const u8` to integer `usize` + --> $DIR/lint-strict-provenance-lossy-casts.rs:14:20 + | +LL | let ptr_addr = ptr as usize; + | ^^^--------- + | | + | help: use `.addr()` to obtain the address of a pointer: `.addr()` | = help: if you can't comply with strict provenance and need to expose the pointer provenance you can use `.expose_addr()` instead -error: aborting due to 2 previous errors +error: under strict provenance it is considered bad style to cast pointer `*const u8` to integer `u32` + --> $DIR/lint-strict-provenance-lossy-casts.rs:16:26 + | +LL | let ptr_addr_32bit = ptr as u32; + | ^^^------- + | | + | help: use `.addr()` to obtain the address of a pointer: `.addr() as u32` + | + = help: if you can't comply with strict provenance and need to expose the pointer provenance you can use `.expose_addr()` instead + +error: aborting due to 4 previous errors From 1d63d6db558b1ff6cd02cb471b51957657c2d71f Mon Sep 17 00:00:00 2001 From: niluxv Date: Sat, 16 Apr 2022 10:18:31 +0200 Subject: [PATCH 4/7] Improve `fuzzy_provenance_casts` lint diagnostics Use `multipart_suggestion` instead of getting a snippet. --- compiler/rustc_typeck/src/check/cast.rs | 16 ++++++---------- .../lint-strict-provenance-fuzzy-casts.stderr | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_typeck/src/check/cast.rs b/compiler/rustc_typeck/src/check/cast.rs index da054077376..a153997599a 100644 --- a/compiler/rustc_typeck/src/check/cast.rs +++ b/compiler/rustc_typeck/src/check/cast.rs @@ -1041,16 +1041,12 @@ impl<'a, 'tcx> CastCheck<'tcx> { self.expr_ty, self.cast_ty )); let msg = "use `.with_addr()` to adjust a valid pointer in the same allocation, to this address"; - if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) { - err.span_suggestion( - self.span, - msg, - format!("(...).with_addr({snippet})"), - Applicability::HasPlaceholders, - ); - } else { - err.help(msg); - } + let suggestions = vec![ + (self.expr_span.shrink_to_lo(), String::from("(...).with_addr(")), + (self.expr_span.shrink_to_hi().to(self.cast_span), String::from(")")), + ]; + + err.multipart_suggestion(msg, suggestions, Applicability::MaybeIncorrect); err.help( "if you can't comply with strict provenance and don't have a pointer with \ the correct provenance you can use `std::ptr::from_exposed_addr()` instead" diff --git a/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr b/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr index e50d243b6ad..c85934aa3ba 100644 --- a/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr +++ b/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr @@ -13,7 +13,7 @@ LL | #![deny(fuzzy_provenance_casts)] help: use `.with_addr()` to adjust a valid pointer in the same allocation, to this address | LL | let dangling = (...).with_addr(16_usize); - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++++ ~ error: aborting due to previous error From 54353203e4742c28a4d85933cbcdc8f10a17631d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 16 Apr 2022 09:11:31 -0400 Subject: [PATCH 5/7] update Miri --- src/tools/miri | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/miri b/src/tools/miri index c568f32f165..1ef91e12277 160000 --- a/src/tools/miri +++ b/src/tools/miri @@ -1 +1 @@ -Subproject commit c568f32f165d86aba51ec544756c3c833acbabd7 +Subproject commit 1ef91e122775060acb1fbda2c9a366891af3ea89 From e6a87208071bc21bc3c5600e64f2fcd44245d805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20BRANSTETT?= Date: Mon, 11 Apr 2022 12:28:56 +0200 Subject: [PATCH 6/7] htmldocck: Compare HTML tree instead of plain text html --- src/etc/htmldocck.py | 70 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/src/etc/htmldocck.py b/src/etc/htmldocck.py index 6bb235b2c83..df215f31823 100644 --- a/src/etc/htmldocck.py +++ b/src/etc/htmldocck.py @@ -285,6 +285,11 @@ def flatten(node): return ''.join(acc) +def make_xml(text): + xml = ET.XML('%s' % text) + return xml + + def normalize_xpath(path): path = path.replace("{{channel}}", channel) if path.startswith('//'): @@ -401,7 +406,7 @@ def get_tree_count(tree, path): return len(tree.findall(path)) -def check_snapshot(snapshot_name, tree, normalize_to_text): +def check_snapshot(snapshot_name, actual_tree, normalize_to_text): assert rust_test_path.endswith('.rs') snapshot_path = '{}.{}.{}'.format(rust_test_path[:-3], snapshot_name, 'html') try: @@ -414,11 +419,15 @@ def check_snapshot(snapshot_name, tree, normalize_to_text): raise FailedCheck('No saved snapshot value') if not normalize_to_text: - actual_str = ET.tostring(tree).decode('utf-8') + actual_str = ET.tostring(actual_tree).decode('utf-8') else: - actual_str = flatten(tree) + actual_str = flatten(actual_tree) + + if not expected_str \ + or (not normalize_to_text and + not compare_tree(make_xml(actual_str), make_xml(expected_str), stderr)) \ + or (normalize_to_text and actual_str != expected_str): - if expected_str != actual_str: if bless: with open(snapshot_path, 'w') as snapshot_file: snapshot_file.write(actual_str) @@ -430,6 +439,59 @@ def check_snapshot(snapshot_name, tree, normalize_to_text): print() raise FailedCheck('Actual snapshot value is different than expected') + +# Adapted from https://github.com/formencode/formencode/blob/3a1ba9de2fdd494dd945510a4568a3afeddb0b2e/formencode/doctest_xml_compare.py#L72-L120 +def compare_tree(x1, x2, reporter=None): + if x1.tag != x2.tag: + if reporter: + reporter('Tags do not match: %s and %s' % (x1.tag, x2.tag)) + return False + for name, value in x1.attrib.items(): + if x2.attrib.get(name) != value: + if reporter: + reporter('Attributes do not match: %s=%r, %s=%r' + % (name, value, name, x2.attrib.get(name))) + return False + for name in x2.attrib: + if name not in x1.attrib: + if reporter: + reporter('x2 has an attribute x1 is missing: %s' + % name) + return False + if not text_compare(x1.text, x2.text): + if reporter: + reporter('text: %r != %r' % (x1.text, x2.text)) + return False + if not text_compare(x1.tail, x2.tail): + if reporter: + reporter('tail: %r != %r' % (x1.tail, x2.tail)) + return False + cl1 = list(x1) + cl2 = list(x2) + if len(cl1) != len(cl2): + if reporter: + reporter('children length differs, %i != %i' + % (len(cl1), len(cl2))) + return False + i = 0 + for c1, c2 in zip(cl1, cl2): + i += 1 + if not compare_tree(c1, c2, reporter=reporter): + if reporter: + reporter('children %i do not match: %s' + % (i, c1.tag)) + return False + return True + + +def text_compare(t1, t2): + if not t1 and not t2: + return True + if t1 == '*' or t2 == '*': + return True + return (t1 or '').strip() == (t2 or '').strip() + + def stderr(*args): if sys.version_info.major < 3: file = codecs.getwriter('utf-8')(sys.stderr) From bf02d1ea5ff52c26db8c13d80bc142de53b65e76 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sat, 16 Apr 2022 19:30:23 +0100 Subject: [PATCH 7/7] No need to check the assert all the time. --- library/core/src/char/methods.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 5809ed1f33b..0bec38a877e 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -343,10 +343,10 @@ impl char { without modifying the original"] #[inline] pub const fn to_digit(self, radix: u32) -> Option { - assert!(radix <= 36, "to_digit: radix is too high (maximum 36)"); // If not a digit, a number greater than radix will be created. let mut digit = (self as u32).wrapping_sub('0' as u32); if radix > 10 { + assert!(radix <= 36, "to_digit: radix is too high (maximum 36)"); if digit < 10 { return Some(digit); }