diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 9dbd59506b188..a1487aa0060d5 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -70,6 +70,17 @@ impl mut_visit::MutVisitor for TokenStripper { i.tokens = None; mut_visit::noop_flat_map_foreign_item(i, self) } + fn flat_map_trait_item( + &mut self, + mut i: P, + ) -> SmallVec<[P; 1]> { + i.tokens = None; + mut_visit::noop_flat_map_assoc_item(i, self) + } + fn flat_map_impl_item(&mut self, mut i: P) -> SmallVec<[P; 1]> { + i.tokens = None; + mut_visit::noop_flat_map_assoc_item(i, self) + } fn visit_block(&mut self, b: &mut P) { b.tokens = None; mut_visit::noop_visit_block(b, self); diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 2704aab322924..07c23d29e20a6 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -2188,7 +2188,8 @@ impl BTreeMap { /// assert_eq!(a.len(), 1); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn len(&self) -> usize { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn len(&self) -> usize { self.length } @@ -2207,7 +2208,8 @@ impl BTreeMap { /// assert!(!a.is_empty()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_empty(&self) -> bool { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn is_empty(&self) -> bool { self.len() == 0 } diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index adb94972f5bb6..09aabdcd0fb27 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -1527,6 +1527,13 @@ fn test_send() { } } +#[allow(dead_code)] +fn test_const() { + const MAP: &'static BTreeMap<(), ()> = &BTreeMap::new(); + const LEN: usize = MAP.len(); + const IS_EMPTY: bool = MAP.is_empty(); +} + #[test] fn test_occupied_entry_key() { let mut a = BTreeMap::new(); diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 3ad74969bec33..684019f8f5f5e 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -950,7 +950,8 @@ impl BTreeSet { /// assert_eq!(v.len(), 1); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn len(&self) -> usize { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn len(&self) -> usize { self.map.len() } @@ -967,7 +968,8 @@ impl BTreeSet { /// assert!(!v.is_empty()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_empty(&self) -> bool { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn is_empty(&self) -> bool { self.len() == 0 } } diff --git a/library/alloc/src/collections/btree/set/tests.rs b/library/alloc/src/collections/btree/set/tests.rs index 9267435728216..2069cde4dba3b 100644 --- a/library/alloc/src/collections/btree/set/tests.rs +++ b/library/alloc/src/collections/btree/set/tests.rs @@ -15,6 +15,13 @@ fn test_clone_eq() { assert_eq!(m.clone(), m); } +#[allow(dead_code)] +fn test_const() { + const SET: &'static BTreeSet<()> = &BTreeSet::new(); + const LEN: usize = SET.len(); + const IS_EMPTY: bool = SET.is_empty(); +} + #[test] fn test_iter_min_max() { let mut a = BTreeSet::new(); diff --git a/library/std/src/f32.rs b/library/std/src/f32.rs index db6255de90634..2a54b117ff4f8 100644 --- a/library/std/src/f32.rs +++ b/library/std/src/f32.rs @@ -719,12 +719,13 @@ impl f32 { /// # Examples /// /// ``` - /// let x = 6.0f32; + /// let x = 1e-8_f32; /// - /// // e^(ln(6)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 5.0).abs(); + /// // for very small x, e^x is approximately 1 + x + x^2 / 2 + /// let approx = x + x * x / 2.0; + /// let abs_difference = (x.exp_m1() - approx).abs(); /// - /// assert!(abs_difference <= f32::EPSILON); + /// assert!(abs_difference < 1e-10); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] @@ -739,12 +740,13 @@ impl f32 { /// # Examples /// /// ``` - /// let x = std::f32::consts::E - 1.0; + /// let x = 1e-8_f32; /// - /// // ln(1 + (e - 1)) == ln(e) == 1 - /// let abs_difference = (x.ln_1p() - 1.0).abs(); + /// // for very small x, ln(1 + x) is approximately x - x^2 / 2 + /// let approx = x - x * x / 2.0; + /// let abs_difference = (x.ln_1p() - approx).abs(); /// - /// assert!(abs_difference <= f32::EPSILON); + /// assert!(abs_difference < 1e-10); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/f64.rs b/library/std/src/f64.rs index 966a0e8e183ae..363d1a0047613 100644 --- a/library/std/src/f64.rs +++ b/library/std/src/f64.rs @@ -721,12 +721,13 @@ impl f64 { /// # Examples /// /// ``` - /// let x = 7.0_f64; + /// let x = 1e-16_f64; /// - /// // e^(ln(7)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 6.0).abs(); + /// // for very small x, e^x is approximately 1 + x + x^2 / 2 + /// let approx = x + x * x / 2.0; + /// let abs_difference = (x.exp_m1() - approx).abs(); /// - /// assert!(abs_difference < 1e-10); + /// assert!(abs_difference < 1e-20); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] @@ -741,12 +742,13 @@ impl f64 { /// # Examples /// /// ``` - /// let x = std::f64::consts::E - 1.0; + /// let x = 1e-16_f64; /// - /// // ln(1 + (e - 1)) == ln(e) == 1 - /// let abs_difference = (x.ln_1p() - 1.0).abs(); + /// // for very small x, ln(1 + x) is approximately x - x^2 / 2 + /// let approx = x - x * x / 2.0; + /// let abs_difference = (x.ln_1p() - approx).abs(); /// - /// assert!(abs_difference < 1e-10); + /// assert!(abs_difference < 1e-20); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 4281867314cca..d18b94b6c1aef 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -23,6 +23,20 @@ pub use crate::panicking::{set_hook, take_hook}; #[stable(feature = "panic_hooks", since = "1.10.0")] pub use core::panic::{Location, PanicInfo}; +/// Panic the current thread with the given message as the panic payload. +/// +/// The message can be of any (`Any + Send`) type, not just strings. +/// +/// The message is wrapped in a `Box<'static + Any + Send>`, which can be +/// accessed later using [`PanicInfo::payload`]. +/// +/// See the [`panic!`] macro for more information about panicking. +#[unstable(feature = "panic_any", issue = "78500")] +#[inline] +pub fn panic_any(msg: M) -> ! { + crate::panicking::begin_panic(msg); +} + /// A marker trait which represents "panic safe" types in Rust. /// /// This trait is implemented by default for many types and behaves similarly in diff --git a/library/std/src/sys/unix/net.rs b/library/std/src/sys/unix/net.rs index 74c7db27226ef..71c6aa5a0e7ea 100644 --- a/library/std/src/sys/unix/net.rs +++ b/library/std/src/sys/unix/net.rs @@ -55,9 +55,18 @@ impl Socket { pub fn new_raw(fam: c_int, ty: c_int) -> io::Result { unsafe { cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { - // On Linux we pass the SOCK_CLOEXEC flag to atomically create - // the socket and set it as CLOEXEC, added in 2.6.27. + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { + // On platforms that support it we pass the SOCK_CLOEXEC + // flag to atomically create the socket and set it as + // CLOEXEC. On Linux this was added in 2.6.27. let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?; Ok(Socket(FileDesc::new(fd))) } else { @@ -83,7 +92,15 @@ impl Socket { let mut fds = [0, 0]; cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { // Like above, set cloexec atomically cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?; Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1])))) @@ -174,9 +191,18 @@ impl Socket { pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result { // Unfortunately the only known way right now to accept a socket and // atomically set the CLOEXEC flag is to use the `accept4` syscall on - // Linux. This was added in 2.6.28, glibc 2.10 and musl 0.9.5. + // platforms that support it. On Linux, this was added in 2.6.28, + // glibc 2.10 and musl 0.9.5. cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { let fd = cvt_r(|| unsafe { libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC) })?; diff --git a/src/bootstrap/setup.rs b/src/bootstrap/setup.rs index c6e1c99564c09..f5ce45a5bd11b 100644 --- a/src/bootstrap/setup.rs +++ b/src/bootstrap/setup.rs @@ -89,7 +89,7 @@ pub fn setup(src_path: &Path, profile: Profile) { std::process::exit(1); } - let path = cfg_file.unwrap_or_else(|| src_path.join("config.toml")); + let path = cfg_file.unwrap_or("config.toml".into()); let settings = format!( "# Includes one of the default files in src/bootstrap/defaults\n\ profile = \"{}\"\n\ diff --git a/src/test/ui/ast-json/issue-78510-assoc-ice.rs b/src/test/ui/ast-json/issue-78510-assoc-ice.rs new file mode 100644 index 0000000000000..ef3117c49cad3 --- /dev/null +++ b/src/test/ui/ast-json/issue-78510-assoc-ice.rs @@ -0,0 +1,18 @@ +// compile-flags: -Zast-json +// +// Regression test for issue #78510 +// Tests that we don't ICE when we have tokens for an associated item + +struct S; + +impl S { + #[derive(Debug)] //~ ERROR `derive` may only be applied to structs, enums and unions + fn f() {} +} + +trait Bar { + #[derive(Debug)] //~ ERROR `derive` may only be applied to structs, enums and unions + fn foo() {} +} + +fn main() {} diff --git a/src/test/ui/ast-json/issue-78510-assoc-ice.stderr b/src/test/ui/ast-json/issue-78510-assoc-ice.stderr new file mode 100644 index 0000000000000..3573c203a7893 --- /dev/null +++ b/src/test/ui/ast-json/issue-78510-assoc-ice.stderr @@ -0,0 +1,15 @@ +error[E0774]: `derive` may only be applied to structs, enums and unions + --> $DIR/issue-78510-assoc-ice.rs:9:5 + | +LL | #[derive(Debug)] + | ^^^^^^^^^^^^^^^^ + +error[E0774]: `derive` may only be applied to structs, enums and unions + --> $DIR/issue-78510-assoc-ice.rs:14:5 + | +LL | #[derive(Debug)] + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0774`. diff --git a/src/test/ui/ast-json/issue-78510-assoc-ice.stdout b/src/test/ui/ast-json/issue-78510-assoc-ice.stdout new file mode 100644 index 0000000000000..fef9504285b53 --- /dev/null +++ b/src/test/ui/ast-json/issue-78510-assoc-ice.stdout @@ -0,0 +1 @@ +{"module":{"inner":{"lo":139,"hi":397},"unsafety":"No","items":[{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"prelude_import","span":{"lo":0,"hi":0}},"id":3,"args":null}],"tokens":null},"args":"Empty","tokens":null}]},"id":null,"style":"Outer","span":{"lo":0,"hi":0},"tokens":null}],"id":4,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"","span":{"lo":0,"hi":0}},"kind":{"variant":"Use","fields":[{"prefix":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"{{root}}","span":{"lo":0,"hi":0}},"id":5,"args":null},{"ident":{"name":"std","span":{"lo":0,"hi":0}},"id":6,"args":null},{"ident":{"name":"prelude","span":{"lo":0,"hi":0}},"id":7,"args":null},{"ident":{"name":"v1","span":{"lo":0,"hi":0}},"id":8,"args":null}],"tokens":null},"kind":"Glob","span":{"lo":0,"hi":0}}]},"tokens":null},{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"macro_use","span":{"lo":0,"hi":0}},"id":9,"args":null}],"tokens":null},"args":"Empty","tokens":null}]},"id":null,"style":"Outer","span":{"lo":0,"hi":0},"tokens":null}],"id":10,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"std","span":{"lo":0,"hi":0}},"kind":{"variant":"ExternCrate","fields":[null]},"tokens":null},{"attrs":[],"id":11,"span":{"lo":139,"hi":148},"vis":{"kind":"Inherited","span":{"lo":139,"hi":139},"tokens":null},"ident":{"name":"S","span":{"lo":146,"hi":147}},"kind":{"variant":"Struct","fields":[{"variant":"Unit","fields":[12]},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":147,"hi":147}},"span":{"lo":147,"hi":147}}]},"tokens":null},{"attrs":[],"id":13,"span":{"lo":150,"hi":263},"vis":{"kind":"Inherited","span":{"lo":150,"hi":150},"tokens":null},"ident":{"name":"","span":{"lo":0,"hi":0}},"kind":{"variant":"Impl","fields":["No","Positive","Final","No",{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":156,"hi":156}},"span":{"lo":154,"hi":154}},null,{"id":14,"kind":{"variant":"Path","fields":[null,{"span":{"lo":155,"hi":156},"segments":[{"ident":{"name":"S","span":{"lo":155,"hi":156}},"id":15,"args":null}],"tokens":null}]},"span":{"lo":155,"hi":156},"tokens":null},[{"attrs":[],"id":19,"span":{"lo":252,"hi":261},"vis":{"kind":"Inherited","span":{"lo":252,"hi":252},"tokens":null},"ident":{"name":"f","span":{"lo":255,"hi":256}},"kind":{"variant":"Fn","fields":["Final",{"header":{"unsafety":"No","asyncness":"No","constness":"No","ext":"None"},"decl":{"inputs":[],"output":{"variant":"Default","fields":[{"lo":259,"hi":259}]}},"span":{"lo":252,"hi":258}},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":258,"hi":258}},"span":{"lo":256,"hi":256}},{"stmts":[],"id":20,"rules":"Default","span":{"lo":259,"hi":261},"tokens":null}]},"tokens":null}]]},"tokens":null},{"attrs":[],"id":16,"span":{"lo":265,"hi":383},"vis":{"kind":"Inherited","span":{"lo":265,"hi":265},"tokens":null},"ident":{"name":"Bar","span":{"lo":271,"hi":274}},"kind":{"variant":"Trait","fields":["No","No",{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":274,"hi":274}},"span":{"lo":274,"hi":274}},[],[{"attrs":[],"id":21,"span":{"lo":370,"hi":381},"vis":{"kind":"Inherited","span":{"lo":370,"hi":370},"tokens":null},"ident":{"name":"foo","span":{"lo":373,"hi":376}},"kind":{"variant":"Fn","fields":["Final",{"header":{"unsafety":"No","asyncness":"No","constness":"No","ext":"None"},"decl":{"inputs":[],"output":{"variant":"Default","fields":[{"lo":379,"hi":379}]}},"span":{"lo":370,"hi":378}},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":378,"hi":378}},"span":{"lo":376,"hi":376}},{"stmts":[],"id":22,"rules":"Default","span":{"lo":379,"hi":381},"tokens":null}]},"tokens":null}]]},"tokens":null},{"attrs":[],"id":17,"span":{"lo":385,"hi":397},"vis":{"kind":"Inherited","span":{"lo":385,"hi":385},"tokens":null},"ident":{"name":"main","span":{"lo":388,"hi":392}},"kind":{"variant":"Fn","fields":["Final",{"header":{"unsafety":"No","asyncness":"No","constness":"No","ext":"None"},"decl":{"inputs":[],"output":{"variant":"Default","fields":[{"lo":395,"hi":395}]}},"span":{"lo":385,"hi":394}},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":394,"hi":394}},"span":{"lo":392,"hi":392}},{"stmts":[],"id":18,"rules":"Default","span":{"lo":395,"hi":397},"tokens":null}]},"tokens":null}],"inline":true},"attrs":[],"span":{"lo":139,"hi":397},"proc_macros":[]}