diff options
author | Joel Galenson <jgalenson@google.com> | 2021-09-23 14:36:04 -0700 |
---|---|---|
committer | Joel Galenson <jgalenson@google.com> | 2021-09-23 14:36:04 -0700 |
commit | 34ed8e92d22843a9f413bf248e892adbf91ebdec (patch) | |
tree | e0eff9bc7d4ae735b0847ffc0027891ca9c8859b | |
parent | 8624dbf1095c20f36ab9d79805ff2ea500a0f12f (diff) | |
parent | 36d9ac1fab726e14305ce1919ebf8a3d64949d30 (diff) | |
download | cxx-34ed8e92d22843a9f413bf248e892adbf91ebdec.tar.gz |
Upgrade rust/cxx to 36d9ac1fab726e14305ce1919ebf8a3d64949d30
Test: make
Change-Id: I45372b36cbe5c64d556df5e0b7ef9451e6c65ea5
59 files changed, 417 insertions, 232 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 60b5974f..0b82cd99 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -96,8 +96,7 @@ jobs: clippy: name: Clippy runs-on: ubuntu-latest - #if: github.event_name != 'pull_request' - if: false # Clippy crash: https://github.com/rust-lang/rust-clippy/issues/7423 + if: github.event_name != 'pull_request' steps: - uses: actions/checkout@v2 - uses: dtolnay/rust-toolchain@clippy @@ -25,19 +25,19 @@ rust_binary( cxx_library( name = "core", srcs = ["src/cxx.cc"], - visibility = ["PUBLIC"], - header_namespace = "rust", exported_headers = { "cxx.h": "include/cxx.h", }, exported_linker_flags = ["-lstdc++"], + header_namespace = "rust", + visibility = ["PUBLIC"], ) rust_library( name = "macro", srcs = glob(["macro/src/**"]), - proc_macro = True, crate = "cxxbridge_macro", + proc_macro = True, deps = [ "//third-party:proc-macro2", "//third-party:quote", @@ -1,5 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("//tools/bazel:rust.bzl", "rust_binary", "rust_library") +load("@rules_rust//rust:rust.bzl", "rust_binary", "rust_library") rust_library( name = "cxx", @@ -1,6 +1,6 @@ [package] name = "cxx" -version = "1.0.51" # remember to update html_root_url +version = "1.0.54" # remember to update html_root_url authors = ["David Tolnay <dtolnay@gmail.com>"] edition = "2018" links = "cxxbridge1" @@ -21,15 +21,15 @@ default = ["cxxbridge-flags/default"] # c++11 "c++20" = ["cxxbridge-flags/c++20"] [dependencies] -cxxbridge-macro = { version = "=1.0.51", path = "macro" } +cxxbridge-macro = { version = "=1.0.54", path = "macro" } link-cplusplus = "1.0" [build-dependencies] cc = "1.0.49" -cxxbridge-flags = { version = "=1.0.51", path = "flags", default-features = false } +cxxbridge-flags = { version = "=1.0.54", path = "flags", default-features = false } [dev-dependencies] -cxx-build = { version = "=1.0.51", path = "gen/build" } +cxx-build = { version = "=1.0.54", path = "gen/build" } cxx-gen = { version = "0.7", path = "gen/lib" } cxx-test-suite = { version = "0", path = "tests/ffi" } rustversion = "1.0" @@ -9,11 +9,11 @@ third_party { type: GIT value: "https://github.com/dtolnay/cxx.git" } - version: "971082d9dc57e581e046b1b10596a97c41287374" + version: "36d9ac1fab726e14305ce1919ebf8a3d64949d30" license_type: NOTICE last_upgrade_date { year: 2021 - month: 8 - day: 9 + month: 9 + day: 23 } } @@ -14,7 +14,7 @@ http_archive( load("@rules_rust//rust:repositories.bzl", "rust_repositories") -RUST_VERSION = "1.51.0" +RUST_VERSION = "1.54.0" rust_repositories( edition = "2018", diff --git a/book/src/binding/string.md b/book/src/binding/string.md index 533894bc..d564e00c 100644 --- a/book/src/binding/string.md +++ b/book/src/binding/string.md @@ -40,6 +40,9 @@ public: const char *c_str() noexcept; + size_t capacity() const noexcept; + void reserve(size_t new_cap) noexcept; + using iterator = char *; iterator begin() noexcept; iterator end() noexcept; diff --git a/book/src/build/bazel.md b/book/src/build/bazel.md index 08edb19f..8f910550 100644 --- a/book/src/build/bazel.md +++ b/book/src/build/bazel.md @@ -70,7 +70,7 @@ def rust_cxx_bridge(name, src, deps = []): # demo/BUILD load("@rules_cc//cc:defs.bzl", "cc_library") -load("//tools/bazel:rust.bzl", "rust_binary") +load("@rules_rust//rust:rust.bzl", "rust_binary") load("//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge") rust_binary( @@ -28,6 +28,12 @@ fn main() { rustc.version, ); } + + if rustc.minor < 52 { + // #![deny(unsafe_op_in_unsafe_fn)]. + // https://github.com/rust-lang/rust/issues/71668 + println!("cargo:rustc-cfg=no_unsafe_op_in_unsafe_fn_lint"); + } } } @@ -1,5 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("//tools/bazel:rust.bzl", "rust_binary") +load("@rules_rust//rust:rust.bzl", "rust_binary") load("//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge") rust_binary( diff --git a/flags/Cargo.toml b/flags/Cargo.toml index 59fb3910..baf1157a 100644 --- a/flags/Cargo.toml +++ b/flags/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cxxbridge-flags" -version = "1.0.51" +version = "1.0.54" authors = ["David Tolnay <dtolnay@gmail.com>"] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/gen/build/Cargo.toml b/gen/build/Cargo.toml index 1d39dedb..393fc1ce 100644 --- a/gen/build/Cargo.toml +++ b/gen/build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cxx-build" -version = "1.0.51" +version = "1.0.54" authors = ["David Tolnay <dtolnay@gmail.com>"] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/gen/build/src/lib.rs b/gen/build/src/lib.rs index 63b2cf63..b8a463c3 100644 --- a/gen/build/src/lib.rs +++ b/gen/build/src/lib.rs @@ -51,6 +51,7 @@ clippy::doc_markdown, clippy::drop_copy, clippy::enum_glob_use, + clippy::if_same_then_else, clippy::inherent_to_string, clippy::items_after_statements, clippy::let_underscore_drop, diff --git a/gen/cmd/Cargo.toml b/gen/cmd/Cargo.toml index 4bb5f38d..4b3a299f 100644 --- a/gen/cmd/Cargo.toml +++ b/gen/cmd/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cxxbridge-cmd" -version = "1.0.51" +version = "1.0.54" authors = ["David Tolnay <dtolnay@gmail.com>"] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/gen/cmd/src/main.rs b/gen/cmd/src/main.rs index f419dad4..1c15db63 100644 --- a/gen/cmd/src/main.rs +++ b/gen/cmd/src/main.rs @@ -3,6 +3,7 @@ clippy::cognitive_complexity, clippy::default_trait_access, clippy::enum_glob_use, + clippy::if_same_then_else, clippy::inherent_to_string, clippy::items_after_statements, clippy::large_enum_variant, diff --git a/gen/lib/Cargo.toml b/gen/lib/Cargo.toml index 3cca49cd..37d81687 100644 --- a/gen/lib/Cargo.toml +++ b/gen/lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cxx-gen" -version = "0.7.51" +version = "0.7.54" authors = ["Adrian Taylor <adetaylor@chromium.org>"] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/gen/lib/src/lib.rs b/gen/lib/src/lib.rs index e3eca5e7..411953b2 100644 --- a/gen/lib/src/lib.rs +++ b/gen/lib/src/lib.rs @@ -12,6 +12,7 @@ clippy::cast_sign_loss, clippy::default_trait_access, clippy::enum_glob_use, + clippy::if_same_then_else, clippy::inherent_to_string, clippy::items_after_statements, clippy::match_bool, diff --git a/gen/src/write.rs b/gen/src/write.rs index c6d59d8b..a3b9c9fa 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -1467,7 +1467,7 @@ fn write_rust_vec_extern(out: &mut OutFile, key: NamedImplKey) { ); writeln!( out, - "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t cap) noexcept;", + "void cxxbridge1$rust_vec${}$reserve_total(::rust::Vec<{}> *ptr, ::std::size_t new_cap) noexcept;", instance, inner, ); writeln!( @@ -1562,12 +1562,12 @@ fn write_rust_vec_impl(out: &mut OutFile, key: NamedImplKey) { begin_function_definition(out); writeln!( out, - "void Vec<{}>::reserve_total(::std::size_t cap) noexcept {{", + "void Vec<{}>::reserve_total(::std::size_t new_cap) noexcept {{", inner, ); writeln!( out, - " return cxxbridge1$rust_vec${}$reserve_total(this, cap);", + " return cxxbridge1$rust_vec${}$reserve_total(this, new_cap);", instance, ); writeln!(out, "}}"); diff --git a/include/cxx.h b/include/cxx.h index 3dab92e8..dffcb01a 100644 --- a/include/cxx.h +++ b/include/cxx.h @@ -59,6 +59,9 @@ public: const char *c_str() noexcept; + std::size_t capacity() const noexcept; + void reserve(size_t new_cap) noexcept; + using iterator = char *; iterator begin() noexcept; iterator end() noexcept; @@ -338,7 +341,7 @@ public: Vec(unsafe_bitcopy_t, const Vec &) noexcept; private: - void reserve_total(std::size_t cap) noexcept; + void reserve_total(std::size_t new_cap) noexcept; void set_len(std::size_t len) noexcept; void drop() noexcept; @@ -540,8 +543,8 @@ bool Slice<T>::empty() const noexcept { template <typename T> T &Slice<T>::operator[](std::size_t n) const noexcept { assert(n < this->size()); - auto pos = static_cast<char *>(slicePtr(this)) + size_of<T>() * n; - return *reinterpret_cast<T *>(pos); + auto ptr = static_cast<char *>(slicePtr(this)) + size_of<T>() * n; + return *reinterpret_cast<T *>(ptr); } template <typename T> @@ -579,8 +582,8 @@ Slice<T>::iterator::operator->() const noexcept { template <typename T> typename Slice<T>::iterator::reference Slice<T>::iterator::operator[]( typename Slice<T>::iterator::difference_type n) const noexcept { - auto pos = static_cast<char *>(this->pos) + this->stride * n; - return *reinterpret_cast<T *>(pos); + auto ptr = static_cast<char *>(this->pos) + this->stride * n; + return *reinterpret_cast<T *>(ptr); } template <typename T> diff --git a/macro/Cargo.toml b/macro/Cargo.toml index 10672b76..65f15c3a 100644 --- a/macro/Cargo.toml +++ b/macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cxxbridge-macro" -version = "1.0.51" +version = "1.0.54" authors = ["David Tolnay <dtolnay@gmail.com>"] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/macro/src/derive.rs b/macro/src/derive.rs index 2f770951..ea36e3e3 100644 --- a/macro/src/derive.rs +++ b/macro/src/derive.rs @@ -151,6 +151,7 @@ fn struct_default(strct: &Struct, span: Span) -> TokenStream { let fields = strct.fields.iter().map(|field| &field.name.rust); quote_spanned! {span=> + #[allow(clippy::derivable_impls)] // different spans than the derived impl impl #generics ::std::default::Default for #ident #generics { fn default() -> Self { #ident { diff --git a/macro/src/expand.rs b/macro/src/expand.rs index bc3f000d..5a879cbd 100644 --- a/macro/src/expand.rs +++ b/macro/src/expand.rs @@ -1127,9 +1127,11 @@ fn expand_rust_function_shim_super( Some(ret) => quote!(#ret), None => quote!(()), }; - let impl_trait = quote_spanned!(result.span=> impl); - let display = quote_spanned!(rangle.span=> ::std::fmt::Display); - quote!(-> ::std::result::Result<#ok, #impl_trait #display>) + // Set spans that result in the `Result<...>` written by the user being + // highlighted as the cause if their error type has no Display impl. + let result_begin = quote_spanned!(result.span=> ::std::result::Result<#ok, impl); + let result_end = quote_spanned!(rangle.span=> ::std::fmt::Display>); + quote!(-> #result_begin #result_end) } else { expand_return_type(&sig.ret) }; @@ -1299,8 +1301,8 @@ fn expand_rust_vec(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl } #[doc(hidden)] #[export_name = #link_reserve_total] - unsafe extern "C" fn #local_reserve_total #impl_generics(this: *mut ::cxx::private::RustVec<#elem #ty_generics>, cap: usize) { - (*this).reserve_total(cap); + unsafe extern "C" fn #local_reserve_total #impl_generics(this: *mut ::cxx::private::RustVec<#elem #ty_generics>, new_cap: usize) { + (*this).reserve_total(new_cap); } #[doc(hidden)] #[export_name = #link_set_len] diff --git a/macro/src/lib.rs b/macro/src/lib.rs index fe5e6e12..324881d8 100644 --- a/macro/src/lib.rs +++ b/macro/src/lib.rs @@ -3,7 +3,7 @@ clippy::default_trait_access, clippy::doc_markdown, clippy::enum_glob_use, - clippy::filter_map, + clippy::if_same_then_else, clippy::inherent_to_string, clippy::items_after_statements, clippy::large_enum_variant, @@ -22,6 +22,13 @@ std::size_t cxxbridge1$cxx_string$length(const std::string &s) noexcept { return s.length(); } +void cxxbridge1$cxx_string$clear(std::string &s) noexcept { s.clear(); } + +void cxxbridge1$cxx_string$reserve_total(std::string &s, + size_t new_cap) noexcept { + s.reserve(new_cap); +} + void cxxbridge1$cxx_string$push(std::string &s, const std::uint8_t *ptr, std::size_t len) noexcept { s.append(reinterpret_cast<const char *>(ptr), len); @@ -38,7 +45,11 @@ bool cxxbridge1$string$from_utf16(rust::String *self, const char16_t *ptr, void cxxbridge1$string$drop(rust::String *self) noexcept; const char *cxxbridge1$string$ptr(const rust::String *self) noexcept; std::size_t cxxbridge1$string$len(const rust::String *self) noexcept; -void cxxbridge1$string$reserve_total(rust::String *self, size_t cap) noexcept; +std::size_t cxxbridge1$string$capacity(const rust::String *self) noexcept; +void cxxbridge1$string$reserve_additional(rust::String *self, + size_t additional) noexcept; +void cxxbridge1$string$reserve_total(rust::String *self, + size_t new_cap) noexcept; // rust::Str void cxxbridge1$str$new(rust::Str *self) noexcept; @@ -156,12 +167,20 @@ bool String::empty() const noexcept { return this->size() == 0; } const char *String::c_str() noexcept { auto len = this->length(); - cxxbridge1$string$reserve_total(this, len + 1); + cxxbridge1$string$reserve_additional(this, 1); auto ptr = this->data(); const_cast<char *>(ptr)[len] = '\0'; return ptr; } +std::size_t String::capacity() const noexcept { + return cxxbridge1$string$capacity(this); +} + +void String::reserve(std::size_t new_cap) noexcept { + cxxbridge1$string$reserve_total(this, new_cap); +} + String::iterator String::begin() noexcept { return const_cast<char *>(this->data()); } @@ -547,7 +566,7 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), const CXX_TYPE *cxxbridge1$rust_vec$##RUST_TYPE##$data( \ const rust::Vec<CXX_TYPE> *ptr) noexcept; \ void cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total( \ - rust::Vec<CXX_TYPE> *ptr, std::size_t cap) noexcept; \ + rust::Vec<CXX_TYPE> *ptr, std::size_t new_cap) noexcept; \ void cxxbridge1$rust_vec$##RUST_TYPE##$set_len(rust::Vec<CXX_TYPE> *ptr, \ std::size_t len) noexcept; @@ -573,8 +592,8 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), return cxxbridge1$rust_vec$##RUST_TYPE##$data(this); \ } \ template <> \ - void Vec<CXX_TYPE>::reserve_total(std::size_t cap) noexcept { \ - cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, cap); \ + void Vec<CXX_TYPE>::reserve_total(std::size_t new_cap) noexcept { \ + cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, new_cap); \ } \ template <> \ void Vec<CXX_TYPE>::set_len(std::size_t len) noexcept { \ diff --git a/src/cxx_string.rs b/src/cxx_string.rs index 126acb18..626c2c85 100644 --- a/src/cxx_string.rs +++ b/src/cxx_string.rs @@ -19,6 +19,10 @@ extern "C" { fn string_data(this: &CxxString) -> *const u8; #[link_name = "cxxbridge1$cxx_string$length"] fn string_length(this: &CxxString) -> usize; + #[link_name = "cxxbridge1$cxx_string$clear"] + fn string_clear(this: Pin<&mut CxxString>); + #[link_name = "cxxbridge1$cxx_string$reserve_total"] + fn string_reserve_total(this: Pin<&mut CxxString>, new_cap: usize); #[link_name = "cxxbridge1$cxx_string$push"] fn string_push(this: Pin<&mut CxxString>, ptr: *const u8, len: usize); } @@ -144,6 +148,49 @@ impl CxxString { String::from_utf8_lossy(self.as_bytes()) } + /// Removes all characters from the string. + /// + /// Matches the behavior of C++ [std::string::clear][clear]. + /// + /// Note: **unlike** the guarantee of Rust's `std::string::String::clear`, + /// the C++ standard does not require that capacity is unchanged by this + /// operation. In practice existing implementations do not change the + /// capacity but all pointers, references, and iterators into the string + /// contents are nevertheless invalidated. + /// + /// [clear]: https://en.cppreference.com/w/cpp/string/basic_string/clear + pub fn clear(self: Pin<&mut Self>) { + unsafe { string_clear(self) } + } + + /// Ensures that this string's capacity is at least `additional` bytes + /// larger than its length. + /// + /// The capacity may be increased by more than `additional` bytes if it + /// chooses, to amortize the cost of frequent reallocations. + /// + /// **The meaning of the argument is not the same as + /// [std::string::reserve][reserve] in C++.** The C++ standard library and + /// Rust standard library both have a `reserve` method on strings, but in + /// C++ code the argument always refers to total capacity, whereas in Rust + /// code it always refers to additional capacity. This API on `CxxString` + /// follows the Rust convention, the same way that for the length accessor + /// we use the Rust conventional `len()` naming and not C++ `size()` or + /// `length()`. + /// + /// # Panics + /// + /// Panics if the new capacity overflows usize. + /// + /// [reserve]: https://en.cppreference.com/w/cpp/string/basic_string/reserve + pub fn reserve(self: Pin<&mut Self>, additional: usize) { + let new_cap = self + .len() + .checked_add(additional) + .expect("CxxString capacity overflow"); + unsafe { string_reserve_total(self, new_cap) } + } + /// Appends a given string slice onto the end of this C++ string. pub fn push_str(self: Pin<&mut Self>, s: &str) { self.push_bytes(s.as_bytes()); @@ -223,9 +270,11 @@ impl StackString { pub unsafe fn init(&mut self, value: impl AsRef<[u8]>) -> Pin<&mut CxxString> { let value = value.as_ref(); - let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>(); - string_init(this, value.as_ptr(), value.len()); - Pin::new_unchecked(&mut *this.as_mut_ptr()) + unsafe { + let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>(); + string_init(this, value.as_ptr(), value.len()); + Pin::new_unchecked(&mut *this.as_mut_ptr()) + } } } diff --git a/src/cxx_vector.rs b/src/cxx_vector.rs index 87a985e6..d1fa23a2 100644 --- a/src/cxx_vector.rs +++ b/src/cxx_vector.rs @@ -86,8 +86,10 @@ where /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at pub unsafe fn get_unchecked(&self, pos: usize) -> &T { let this = self as *const CxxVector<T> as *mut CxxVector<T>; - let ptr = T::__get_unchecked(this, pos) as *const T; - &*ptr + unsafe { + let ptr = T::__get_unchecked(this, pos) as *const T; + &*ptr + } } /// Returns a pinned mutable reference to an element without doing bounds @@ -102,8 +104,10 @@ where /// /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at pub unsafe fn index_unchecked_mut(self: Pin<&mut Self>, pos: usize) -> Pin<&mut T> { - let ptr = T::__get_unchecked(self.get_unchecked_mut(), pos); - Pin::new_unchecked(&mut *ptr) + unsafe { + let ptr = T::__get_unchecked(self.get_unchecked_mut(), pos); + Pin::new_unchecked(&mut *ptr) + } } /// Returns a slice to the underlying contiguous array of elements. @@ -372,7 +376,7 @@ macro_rules! vector_element_by_value_methods { fn __push_back(_: Pin<&mut CxxVector<$ty>>, _: &mut ManuallyDrop<$ty>); } } - __push_back(v, value); + unsafe { __push_back(v, value) } } #[doc(hidden)] unsafe fn __pop_back(v: Pin<&mut CxxVector<$ty>>, out: &mut MaybeUninit<$ty>) { @@ -382,7 +386,7 @@ macro_rules! vector_element_by_value_methods { fn __pop_back(_: Pin<&mut CxxVector<$ty>>, _: &mut MaybeUninit<$ty>); } } - __pop_back(v, out); + unsafe { __pop_back(v, out) } } }; } @@ -415,7 +419,7 @@ macro_rules! impl_vector_element { fn __get_unchecked(_: *mut CxxVector<$ty>, _: usize) -> *mut $ty; } } - __get_unchecked(v, pos) + unsafe { __get_unchecked(v, pos) } } vector_element_by_value_methods!($kind, $segment, $ty); #[doc(hidden)] @@ -439,7 +443,7 @@ macro_rules! impl_vector_element { } } let mut repr = MaybeUninit::uninit(); - __unique_ptr_raw(&mut repr, raw); + unsafe { __unique_ptr_raw(&mut repr, raw) } repr } #[doc(hidden)] @@ -450,7 +454,7 @@ macro_rules! impl_vector_element { fn __unique_ptr_get(this: *const MaybeUninit<*mut c_void>) -> *const CxxVector<$ty>; } } - __unique_ptr_get(&repr) + unsafe { __unique_ptr_get(&repr) } } #[doc(hidden)] unsafe fn __unique_ptr_release(mut repr: MaybeUninit<*mut c_void>) -> *mut CxxVector<Self> { @@ -460,7 +464,7 @@ macro_rules! impl_vector_element { fn __unique_ptr_release(this: *mut MaybeUninit<*mut c_void>) -> *mut CxxVector<$ty>; } } - __unique_ptr_release(&mut repr) + unsafe { __unique_ptr_release(&mut repr) } } #[doc(hidden)] unsafe fn __unique_ptr_drop(mut repr: MaybeUninit<*mut c_void>) { @@ -470,7 +474,7 @@ macro_rules! impl_vector_element { fn __unique_ptr_drop(this: *mut MaybeUninit<*mut c_void>); } } - __unique_ptr_drop(&mut repr); + unsafe { __unique_ptr_drop(&mut repr) } } } }; @@ -364,8 +364,10 @@ //! </table> #![no_std] -#![doc(html_root_url = "https://docs.rs/cxx/1.0.51")] +#![doc(html_root_url = "https://docs.rs/cxx/1.0.54")] #![deny(improper_ctypes, improper_ctypes_definitions, missing_docs)] +#![cfg_attr(not(no_unsafe_op_in_unsafe_fn_lint), deny(unsafe_op_in_unsafe_fn))] +#![cfg_attr(no_unsafe_op_in_unsafe_fn_lint, allow(unused_unsafe))] #![allow(non_camel_case_types)] #![allow( clippy::cognitive_complexity, diff --git a/src/result.rs b/src/result.rs index 4ebeda60..d7a31f02 100644 --- a/src/result.rs +++ b/src/result.rs @@ -28,16 +28,16 @@ where { match result { Ok(ok) => { - ptr::write(ret, ok); + unsafe { ptr::write(ret, ok) } Result { ok: ptr::null() } } - Err(err) => to_c_error(err.to_string()), + Err(err) => unsafe { to_c_error(err.to_string()) }, } } unsafe fn to_c_error(msg: String) -> Result { let mut msg = msg; - msg.as_mut_vec().push(b'\0'); + unsafe { msg.as_mut_vec() }.push(b'\0'); let ptr = msg.as_ptr(); let len = msg.len(); @@ -46,22 +46,24 @@ unsafe fn to_c_error(msg: String) -> Result { fn error(ptr: *const u8, len: usize) -> NonNull<u8>; } - let copy = error(ptr, len); + let copy = unsafe { error(ptr, len) }; let err = PtrLen { ptr: copy, len }; Result { err } } impl Result { pub unsafe fn exception(self) -> StdResult<(), Exception> { - if self.ok.is_null() { - Ok(()) - } else { - let err = self.err; - let slice = slice::from_raw_parts_mut(err.ptr.as_ptr(), err.len); - let s = str::from_utf8_unchecked_mut(slice); - Err(Exception { - what: Box::from_raw(s), - }) + unsafe { + if self.ok.is_null() { + Ok(()) + } else { + let err = self.err; + let slice = slice::from_raw_parts_mut(err.ptr.as_ptr(), err.len); + let s = str::from_utf8_unchecked_mut(slice); + Err(Exception { + what: Box::from_raw(s), + }) + } } } } diff --git a/src/rust_slice.rs b/src/rust_slice.rs index 65fa87d1..06963117 100644 --- a/src/rust_slice.rs +++ b/src/rust_slice.rs @@ -24,15 +24,15 @@ impl RustSlice { } pub unsafe fn as_slice<'a, T>(self) -> &'a [T] { - let ptr = self.as_ptr(); + let ptr = self.as_non_null_ptr().as_ptr(); let len = self.len(); - slice::from_raw_parts(ptr.as_ptr(), len) + unsafe { slice::from_raw_parts(ptr, len) } } pub unsafe fn as_mut_slice<'a, T>(self) -> &'a mut [T] { - let ptr = self.as_ptr(); + let ptr = self.as_non_null_ptr().as_ptr(); let len = self.len(); - slice::from_raw_parts_mut(ptr.as_ptr(), len) + unsafe { slice::from_raw_parts_mut(ptr, len) } } pub(crate) fn from_raw_parts<T>(ptr: NonNull<T>, len: usize) -> Self { @@ -43,7 +43,7 @@ impl RustSlice { unsafe { mem::transmute::<NonNull<[()]>, RustSlice>(NonNull::new_unchecked(ptr)) } } - pub(crate) fn as_ptr<T>(&self) -> NonNull<T> { + pub(crate) fn as_non_null_ptr<T>(&self) -> NonNull<T> { let rust_slice = RustSlice { repr: self.repr }; let repr = unsafe { mem::transmute::<RustSlice, NonNull<[()]>>(rust_slice) }; repr.cast() diff --git a/src/rust_str.rs b/src/rust_str.rs index 65ef6d9d..b1b46e3e 100644 --- a/src/rust_str.rs +++ b/src/rust_str.rs @@ -17,8 +17,10 @@ impl RustStr { } pub unsafe fn as_str<'a>(self) -> &'a str { - let repr = mem::transmute::<RustStr, NonNull<str>>(self); - &*repr.as_ptr() + unsafe { + let repr = mem::transmute::<RustStr, NonNull<str>>(self); + &*repr.as_ptr() + } } } diff --git a/src/rust_vec.rs b/src/rust_vec.rs index e66519fe..9f4484db 100644 --- a/src/rust_vec.rs +++ b/src/rust_vec.rs @@ -56,16 +56,16 @@ impl<T> RustVec<T> { self.as_vec().as_ptr() } - pub fn reserve_total(&mut self, cap: usize) { + pub fn reserve_total(&mut self, new_cap: usize) { let vec = self.as_mut_vec(); - let len = vec.len(); - if cap > len { - vec.reserve(cap - len); + if new_cap > vec.capacity() { + let additional = new_cap - vec.len(); + vec.reserve(additional); } } pub unsafe fn set_len(&mut self, len: usize) { - self.as_mut_vec().set_len(len); + unsafe { self.as_mut_vec().set_len(len) } } } diff --git a/src/shared_ptr.rs b/src/shared_ptr.rs index 20c7e0f0..317773d4 100644 --- a/src/shared_ptr.rs +++ b/src/shared_ptr.rs @@ -216,7 +216,7 @@ macro_rules! impl_shared_ptr_target { fn __null(new: *mut c_void); } } - __null(new); + unsafe { __null(new) } } #[doc(hidden)] unsafe fn __new(value: Self, new: *mut c_void) { @@ -226,7 +226,7 @@ macro_rules! impl_shared_ptr_target { fn __uninit(new: *mut c_void) -> *mut c_void; } } - __uninit(new).cast::<$ty>().write(value); + unsafe { __uninit(new).cast::<$ty>().write(value) } } #[doc(hidden)] unsafe fn __clone(this: *const c_void, new: *mut c_void) { @@ -236,7 +236,7 @@ macro_rules! impl_shared_ptr_target { fn __clone(this: *const c_void, new: *mut c_void); } } - __clone(this, new); + unsafe { __clone(this, new) } } #[doc(hidden)] unsafe fn __get(this: *const c_void) -> *const Self { @@ -246,7 +246,7 @@ macro_rules! impl_shared_ptr_target { fn __get(this: *const c_void) -> *const c_void; } } - __get(this).cast() + unsafe { __get(this) }.cast() } #[doc(hidden)] unsafe fn __drop(this: *mut c_void) { @@ -256,7 +256,7 @@ macro_rules! impl_shared_ptr_target { fn __drop(this: *mut c_void); } } - __drop(this); + unsafe { __drop(this) } } } }; diff --git a/src/symbols/exception.rs b/src/symbols/exception.rs index 0c1bb876..cf0701ba 100644 --- a/src/symbols/exception.rs +++ b/src/symbols/exception.rs @@ -4,7 +4,7 @@ use core::slice; #[export_name = "cxxbridge1$exception"] unsafe extern "C" fn exception(ptr: *const u8, len: usize) -> *const u8 { - let slice = slice::from_raw_parts(ptr, len); + let slice = unsafe { slice::from_raw_parts(ptr, len) }; let boxed = String::from_utf8_lossy(slice).into_owned().into_boxed_str(); Box::leak(boxed).as_ptr() } diff --git a/src/symbols/rust_slice.rs b/src/symbols/rust_slice.rs index b6cbce6e..df215acf 100644 --- a/src/symbols/rust_slice.rs +++ b/src/symbols/rust_slice.rs @@ -4,13 +4,14 @@ use core::ptr::{self, NonNull}; #[export_name = "cxxbridge1$slice$new"] unsafe extern "C" fn slice_new(this: &mut MaybeUninit<RustSlice>, ptr: NonNull<()>, len: usize) { + let this = this.as_mut_ptr(); let rust_slice = RustSlice::from_raw_parts(ptr, len); - ptr::write(this.as_mut_ptr(), rust_slice); + unsafe { ptr::write(this, rust_slice) } } #[export_name = "cxxbridge1$slice$ptr"] unsafe extern "C" fn slice_ptr(this: &RustSlice) -> NonNull<()> { - this.as_ptr() + this.as_non_null_ptr() } #[export_name = "cxxbridge1$slice$len"] diff --git a/src/symbols/rust_str.rs b/src/symbols/rust_str.rs index a9e84efb..3d5ec344 100644 --- a/src/symbols/rust_str.rs +++ b/src/symbols/rust_str.rs @@ -6,20 +6,24 @@ use core::str; #[export_name = "cxxbridge1$str$new"] unsafe extern "C" fn str_new(this: &mut MaybeUninit<&str>) { - ptr::write(this.as_mut_ptr(), ""); + let this = this.as_mut_ptr(); + unsafe { ptr::write(this, "") } } #[export_name = "cxxbridge1$str$ref"] unsafe extern "C" fn str_ref<'a>(this: &mut MaybeUninit<&'a str>, string: &'a String) { - ptr::write(this.as_mut_ptr(), string.as_str()); + let this = this.as_mut_ptr(); + let s = string.as_str(); + unsafe { ptr::write(this, s) } } #[export_name = "cxxbridge1$str$from"] unsafe extern "C" fn str_from(this: &mut MaybeUninit<&str>, ptr: *const u8, len: usize) -> bool { - let slice = slice::from_raw_parts(ptr, len); + let slice = unsafe { slice::from_raw_parts(ptr, len) }; match str::from_utf8(slice) { Ok(s) => { - ptr::write(this.as_mut_ptr(), s); + let this = this.as_mut_ptr(); + unsafe { ptr::write(this, s) } true } Err(_) => false, diff --git a/src/symbols/rust_string.rs b/src/symbols/rust_string.rs index 29bcf85d..49d40697 100644 --- a/src/symbols/rust_string.rs +++ b/src/symbols/rust_string.rs @@ -7,12 +7,16 @@ use core::str; #[export_name = "cxxbridge1$string$new"] unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) { - ptr::write(this.as_mut_ptr(), String::new()); + let this = this.as_mut_ptr(); + let new = String::new(); + unsafe { ptr::write(this, new) } } #[export_name = "cxxbridge1$string$clone"] unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) { - ptr::write(this.as_mut_ptr(), other.clone()); + let this = this.as_mut_ptr(); + let clone = other.clone(); + unsafe { ptr::write(this, clone) } } #[export_name = "cxxbridge1$string$from_utf8"] @@ -21,10 +25,12 @@ unsafe extern "C" fn string_from_utf8( ptr: *const u8, len: usize, ) -> bool { - let slice = slice::from_raw_parts(ptr, len); + let slice = unsafe { slice::from_raw_parts(ptr, len) }; match str::from_utf8(slice) { Ok(s) => { - ptr::write(this.as_mut_ptr(), s.to_owned()); + let this = this.as_mut_ptr(); + let owned = s.to_owned(); + unsafe { ptr::write(this, owned) } true } Err(_) => false, @@ -37,10 +43,11 @@ unsafe extern "C" fn string_from_utf16( ptr: *const u16, len: usize, ) -> bool { - let slice = slice::from_raw_parts(ptr, len); + let slice = unsafe { slice::from_raw_parts(ptr, len) }; match String::from_utf16(slice) { Ok(s) => { - ptr::write(this.as_mut_ptr(), s); + let this = this.as_mut_ptr(); + unsafe { ptr::write(this, s) } true } Err(_) => false, @@ -49,7 +56,7 @@ unsafe extern "C" fn string_from_utf16( #[export_name = "cxxbridge1$string$drop"] unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) { - ManuallyDrop::drop(this); + unsafe { ManuallyDrop::drop(this) } } #[export_name = "cxxbridge1$string$ptr"] @@ -62,7 +69,20 @@ unsafe extern "C" fn string_len(this: &String) -> usize { this.len() } +#[export_name = "cxxbridge1$string$capacity"] +unsafe extern "C" fn string_capacity(this: &String) -> usize { + this.capacity() +} + +#[export_name = "cxxbridge1$string$reserve_additional"] +unsafe extern "C" fn string_reserve_additional(this: &mut String, additional: usize) { + this.reserve(additional); +} + #[export_name = "cxxbridge1$string$reserve_total"] -unsafe extern "C" fn string_reserve_total(this: &mut String, cap: usize) { - this.reserve(cap); +unsafe extern "C" fn string_reserve_total(this: &mut String, new_cap: usize) { + if new_cap > this.capacity() { + let additional = new_cap - this.len(); + this.reserve(additional); + } } diff --git a/src/symbols/rust_vec.rs b/src/symbols/rust_vec.rs index 9bb4c293..6f2dab9d 100644 --- a/src/symbols/rust_vec.rs +++ b/src/symbols/rust_vec.rs @@ -15,43 +15,43 @@ macro_rules! rust_vec_shims { attr! { #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")] unsafe extern "C" fn __new(this: *mut RustVec<$ty>) { - ptr::write(this, RustVec::new()); + unsafe { ptr::write(this, RustVec::new()) } } } attr! { #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")] unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) { - ptr::drop_in_place(this); + unsafe { ptr::drop_in_place(this) } } } attr! { #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")] unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize { - (*this).len() + unsafe { &*this }.len() } } attr! { #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$capacity")] unsafe extern "C" fn __capacity(this: *const RustVec<$ty>) -> usize { - (*this).capacity() + unsafe { &*this }.capacity() } } attr! { #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")] unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty { - (*this).as_ptr() + unsafe { &*this }.as_ptr() } } attr! { #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")] - unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, cap: usize) { - (*this).reserve_total(cap); + unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, new_cap: usize) { + unsafe { &mut *this }.reserve_total(new_cap); } } attr! { #[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")] unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) { - (*this).set_len(len); + unsafe { (*this).set_len(len) } } } }; diff --git a/src/unique_ptr.rs b/src/unique_ptr.rs index d9ac4429..63a1ca78 100644 --- a/src/unique_ptr.rs +++ b/src/unique_ptr.rs @@ -103,7 +103,7 @@ where /// twice on the same raw pointer. pub unsafe fn from_raw(raw: *mut T) -> Self { UniquePtr { - repr: T::__raw(raw), + repr: unsafe { T::__raw(raw) }, ty: PhantomData, } } @@ -256,20 +256,20 @@ unsafe impl UniquePtrTarget for CxxString { #[doc(hidden)] unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> { let mut repr = MaybeUninit::uninit(); - unique_ptr_std_string_raw(&mut repr, raw); + unsafe { unique_ptr_std_string_raw(&mut repr, raw) } repr } #[doc(hidden)] unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self { - unique_ptr_std_string_get(&repr) + unsafe { unique_ptr_std_string_get(&repr) } } #[doc(hidden)] unsafe fn __release(mut repr: MaybeUninit<*mut c_void>) -> *mut Self { - unique_ptr_std_string_release(&mut repr) + unsafe { unique_ptr_std_string_release(&mut repr) } } #[doc(hidden)] unsafe fn __drop(mut repr: MaybeUninit<*mut c_void>) { - unique_ptr_std_string_drop(&mut repr); + unsafe { unique_ptr_std_string_drop(&mut repr) } } } @@ -287,18 +287,18 @@ where } #[doc(hidden)] unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> { - T::__unique_ptr_raw(raw) + unsafe { T::__unique_ptr_raw(raw) } } #[doc(hidden)] unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self { - T::__unique_ptr_get(repr) + unsafe { T::__unique_ptr_get(repr) } } #[doc(hidden)] unsafe fn __release(repr: MaybeUninit<*mut c_void>) -> *mut Self { - T::__unique_ptr_release(repr) + unsafe { T::__unique_ptr_release(repr) } } #[doc(hidden)] unsafe fn __drop(repr: MaybeUninit<*mut c_void>) { - T::__unique_ptr_drop(repr); + unsafe { T::__unique_ptr_drop(repr) } } } diff --git a/src/weak_ptr.rs b/src/weak_ptr.rs index 2c06d36a..8a9f1a68 100644 --- a/src/weak_ptr.rs +++ b/src/weak_ptr.rs @@ -126,7 +126,7 @@ macro_rules! impl_weak_ptr_target { fn __null(new: *mut c_void); } } - __null(new); + unsafe { __null(new) } } #[doc(hidden)] unsafe fn __clone(this: *const c_void, new: *mut c_void) { @@ -136,7 +136,7 @@ macro_rules! impl_weak_ptr_target { fn __clone(this: *const c_void, new: *mut c_void); } } - __clone(this, new); + unsafe { __clone(this, new) } } #[doc(hidden)] unsafe fn __downgrade(shared: *const c_void, weak: *mut c_void) { @@ -146,7 +146,7 @@ macro_rules! impl_weak_ptr_target { fn __downgrade(shared: *const c_void, weak: *mut c_void); } } - __downgrade(shared, weak); + unsafe { __downgrade(shared, weak) } } #[doc(hidden)] unsafe fn __upgrade(weak: *const c_void, shared: *mut c_void) { @@ -156,7 +156,7 @@ macro_rules! impl_weak_ptr_target { fn __upgrade(weak: *const c_void, shared: *mut c_void); } } - __upgrade(weak, shared); + unsafe { __upgrade(weak, shared) } } #[doc(hidden)] unsafe fn __drop(this: *mut c_void) { @@ -166,7 +166,7 @@ macro_rules! impl_weak_ptr_target { fn __drop(this: *mut c_void); } } - __drop(this); + unsafe { __drop(this) } } } }; diff --git a/syntax/names.rs b/syntax/names.rs index 8f1204df..7a125ae7 100644 --- a/syntax/names.rs +++ b/syntax/names.rs @@ -9,7 +9,6 @@ use syn::punctuated::Punctuated; #[derive(Clone)] pub struct ForeignName { text: String, - span: Span, } impl Pair { @@ -56,7 +55,7 @@ impl ForeignName { match syn::parse_str::<Ident>(text) { Ok(ident) => { let text = ident.to_string(); - Ok(ForeignName { text, span }) + Ok(ForeignName { text }) } Err(err) => Err(Error::new(span, err)), } diff --git a/tests/BUILD b/tests/BUILD index 7886e4fa..d4af3aff 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -1,5 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("//tools/bazel:rust.bzl", "rust_library", "rust_test") +load("@rules_rust//rust:rust.bzl", "rust_library", "rust_test") load("//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge") rust_test( diff --git a/tests/ffi/tests.cc b/tests/ffi/tests.cc index d153cd16..df7ded07 100644 --- a/tests/ffi/tests.cc +++ b/tests/ffi/tests.cc @@ -835,6 +835,12 @@ extern "C" const char *cxx_run_test() noexcept { ASSERT(cstring == "foo"); ASSERT(other_cstring == "test"); + ASSERT(cstring.capacity() == 3); + cstring.reserve(2); + ASSERT(cstring.capacity() == 3); + cstring.reserve(5); + ASSERT(cstring.capacity() >= 5); + rust::Str cstr = "test"; rust::Str other_cstr = "foo"; swap(cstr, other_cstr); diff --git a/tests/ui/array_len_suffix.stderr b/tests/ui/array_len_suffix.stderr index b72fc022..143bcb04 100644 --- a/tests/ui/array_len_suffix.stderr +++ b/tests/ui/array_len_suffix.stderr @@ -7,4 +7,4 @@ error[E0308]: mismatched types help: change the type of the numeric literal from `u16` to `usize` | 4 | fn array() -> [String; 12usize]; - | ^^^^^^^ + | ~~~~~ diff --git a/tests/ui/expected_named.stderr b/tests/ui/expected_named.stderr index 46764014..dab3b5a3 100644 --- a/tests/ui/expected_named.stderr +++ b/tests/ui/expected_named.stderr @@ -8,4 +8,4 @@ error[E0106]: missing lifetime specifier help: consider using the `'static` lifetime | 5 | fn borrowed() -> UniquePtr<Borrowed<'static>>; - | ^^^^^^^^^^^^^^^^^ + | ~~~~~~~~~~~~~~~~~ diff --git a/tests/ui/opaque_autotraits.stderr b/tests/ui/opaque_autotraits.stderr index 6032c22c..15a2b64c 100644 --- a/tests/ui/opaque_autotraits.stderr +++ b/tests/ui/opaque_autotraits.stderr @@ -1,9 +1,6 @@ error[E0277]: `*const cxx::void` cannot be sent between threads safely --> $DIR/opaque_autotraits.rs:13:5 | -8 | fn assert_send<T: Send>() {} - | ---- required by this bound in `assert_send` -... 13 | assert_send::<ffi::Opaque>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const cxx::void` cannot be sent between threads safely | @@ -15,13 +12,15 @@ note: required because it appears within the type `ffi::Opaque` | 4 | type Opaque; | ^^^^^^ +note: required by a bound in `assert_send` + --> $DIR/opaque_autotraits.rs:8:19 + | +8 | fn assert_send<T: Send>() {} + | ^^^^ required by this bound in `assert_send` error[E0277]: `*const cxx::void` cannot be shared between threads safely --> $DIR/opaque_autotraits.rs:14:5 | -9 | fn assert_sync<T: Sync>() {} - | ---- required by this bound in `assert_sync` -... 14 | assert_sync::<ffi::Opaque>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const cxx::void` cannot be shared between threads safely | @@ -33,13 +32,15 @@ note: required because it appears within the type `ffi::Opaque` | 4 | type Opaque; | ^^^^^^ +note: required by a bound in `assert_sync` + --> $DIR/opaque_autotraits.rs:9:19 + | +9 | fn assert_sync<T: Sync>() {} + | ^^^^ required by this bound in `assert_sync` error[E0277]: `PhantomPinned` cannot be unpinned --> $DIR/opaque_autotraits.rs:15:5 | -10 | fn assert_unpin<T: Unpin>() {} - | ----- required by this bound in `assert_unpin` -... 15 | assert_unpin::<ffi::Opaque>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ within `ffi::Opaque`, the trait `Unpin` is not implemented for `PhantomPinned` | @@ -51,3 +52,8 @@ note: required because it appears within the type `ffi::Opaque` | 4 | type Opaque; | ^^^^^^ +note: required by a bound in `assert_unpin` + --> $DIR/opaque_autotraits.rs:10:20 + | +10 | fn assert_unpin<T: Unpin>() {} + | ^^^^^ required by this bound in `assert_unpin` diff --git a/tests/ui/opaque_not_sized.stderr b/tests/ui/opaque_not_sized.stderr index 6f5dbf38..b50a2699 100644 --- a/tests/ui/opaque_not_sized.stderr +++ b/tests/ui/opaque_not_sized.stderr @@ -2,10 +2,7 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t --> $DIR/opaque_not_sized.rs:4:14 | 4 | type TypeR; - | -----^^^^^- - | | | - | | doesn't have a size known at compile-time - | required by this bound in `__AssertSized` + | ^^^^^ doesn't have a size known at compile-time | = help: within `TypeR`, the trait `Sized` is not implemented for `str` note: required because it appears within the type `TypeR` @@ -13,3 +10,8 @@ note: required because it appears within the type `TypeR` | 8 | struct TypeR(str); | ^^^^^ +note: required by a bound in `__AssertSized` + --> $DIR/opaque_not_sized.rs:4:9 + | +4 | type TypeR; + | ^^^^^^^^^^^ required by this bound in `__AssertSized` diff --git a/tests/ui/rust_pinned.stderr b/tests/ui/rust_pinned.stderr index cbee2c20..8857681c 100644 --- a/tests/ui/rust_pinned.stderr +++ b/tests/ui/rust_pinned.stderr @@ -2,10 +2,7 @@ error[E0277]: `PhantomPinned` cannot be unpinned --> $DIR/rust_pinned.rs:6:14 | 6 | type Pinned; - | -----^^^^^^- - | | | - | | within `Pinned`, the trait `Unpin` is not implemented for `PhantomPinned` - | required by this bound in `__AssertUnpin` + | ^^^^^^ within `Pinned`, the trait `Unpin` is not implemented for `PhantomPinned` | = note: consider using `Box::pin` note: required because it appears within the type `Pinned` @@ -13,3 +10,8 @@ note: required because it appears within the type `Pinned` | 10 | pub struct Pinned { | ^^^^^^ +note: required by a bound in `__AssertUnpin` + --> $DIR/rust_pinned.rs:6:9 + | +6 | type Pinned; + | ^^^^^^^^^^^^ required by this bound in `__AssertUnpin` diff --git a/tests/ui/slice_of_type_alias.stderr b/tests/ui/slice_of_type_alias.stderr index f5fb04b0..aff06f8d 100644 --- a/tests/ui/slice_of_type_alias.stderr +++ b/tests/ui/slice_of_type_alias.stderr @@ -4,7 +4,8 @@ error[E0271]: type mismatch resolving `<ElementOpaque as ExternType>::Kind == Tr 13 | type ElementOpaque = crate::ElementOpaque; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Trivial`, found enum `cxx::kind::Opaque` | - ::: $WORKSPACE/src/extern_type.rs +note: required by a bound in `verify_extern_kind` + --> $DIR/extern_type.rs:186:41 | - | pub fn verify_extern_kind<T: ExternType<Kind = Kind>, Kind: self::Kind>() {} - | ----------- required by this bound in `verify_extern_kind` +186 | pub fn verify_extern_kind<T: ExternType<Kind = Kind>, Kind: self::Kind>() {} + | ^^^^^^^^^^^ required by this bound in `verify_extern_kind` diff --git a/tests/ui/unsupported_elided.stderr b/tests/ui/unsupported_elided.stderr index cd4642c9..8e8a986b 100644 --- a/tests/ui/unsupported_elided.stderr +++ b/tests/ui/unsupported_elided.stderr @@ -16,4 +16,4 @@ error[E0106]: missing lifetime specifier help: consider introducing a named lifetime parameter | 8 | fn f<'a>(t: &'a T) -> &'a str; - | ^^^^ ^^^^^ ^^^ + | ++++ ++ ++ diff --git a/tests/ui/vec_opaque.stderr b/tests/ui/vec_opaque.stderr index b8dad916..3f208fec 100644 --- a/tests/ui/vec_opaque.stderr +++ b/tests/ui/vec_opaque.stderr @@ -16,7 +16,8 @@ error[E0271]: type mismatch resolving `<handle::Job as ExternType>::Kind == Triv 22 | type Job = crate::handle::Job; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Trivial`, found enum `cxx::kind::Opaque` | - ::: $WORKSPACE/src/extern_type.rs +note: required by a bound in `verify_extern_kind` + --> $DIR/extern_type.rs:186:41 | - | pub fn verify_extern_kind<T: ExternType<Kind = Kind>, Kind: self::Kind>() {} - | ----------- required by this bound in `verify_extern_kind` +186 | pub fn verify_extern_kind<T: ExternType<Kind = Kind>, Kind: self::Kind>() {} + | ^^^^^^^^^^^ required by this bound in `verify_extern_kind` diff --git a/tests/ui/vector_autotraits.stderr b/tests/ui/vector_autotraits.stderr index c35d54d2..52fd2a41 100644 --- a/tests/ui/vector_autotraits.stderr +++ b/tests/ui/vector_autotraits.stderr @@ -1,9 +1,6 @@ error[E0277]: `*const cxx::void` cannot be sent between threads safely --> $DIR/vector_autotraits.rs:20:5 | -16 | fn assert_send<T: Send>() {} - | ---- required by this bound in `assert_send` -... 20 | assert_send::<CxxVector<ffi::NotThreadSafe>>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const cxx::void` cannot be sent between threads safely | @@ -18,3 +15,8 @@ note: required because it appears within the type `NotThreadSafe` = note: required because it appears within the type `[NotThreadSafe]` = note: required because it appears within the type `PhantomData<[NotThreadSafe]>` = note: required because it appears within the type `CxxVector<NotThreadSafe>` +note: required by a bound in `assert_send` + --> $DIR/vector_autotraits.rs:16:19 + | +16 | fn assert_send<T: Send>() {} + | ^^^^ required by this bound in `assert_send` diff --git a/tests/ui/wrong_type_id.stderr b/tests/ui/wrong_type_id.stderr index 2d8e50aa..b66bb266 100644 --- a/tests/ui/wrong_type_id.stderr +++ b/tests/ui/wrong_type_id.stderr @@ -4,10 +4,10 @@ error[E0271]: type mismatch resolving `<StringPiece as ExternType>::Id == (f, o, 11 | type ByteRange = crate::here::StringPiece; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 15 elements, found one with 17 elements | - ::: $WORKSPACE/src/extern_type.rs - | - | pub fn verify_extern_type<T: ExternType<Id = Id>, Id>() {} - | ------- required by this bound in `verify_extern_type` - | = note: expected tuple `(f, o, l, l, y, (), B, y, t, e, R, a, n, g, e)` found tuple `(f, o, l, l, y, (), S, t, r, i, n, g, P, i, e, c, e)` +note: required by a bound in `verify_extern_type` + --> $DIR/extern_type.rs:183:41 + | +183 | pub fn verify_extern_type<T: ExternType<Id = Id>, Id>() {} + | ^^^^^^^ required by this bound in `verify_extern_type` diff --git a/third-party/BUCK b/third-party/BUCK index 437bc67c..2633da87 100644 --- a/third-party/BUCK +++ b/third-party/BUCK @@ -1,8 +1,10 @@ # To be generated by Facebook's `reindeer` tool once that is open source. +load("//tools/buck:rust_library.bzl", "rust_library") + rust_library( name = "bitflags", - srcs = glob(["vendor/bitflags-1.2.1/src/**"]), + srcs = glob(["vendor/bitflags-1.3.2/src/**"]), ) rust_library( @@ -42,24 +44,20 @@ rust_library( rust_library( name = "proc-macro2", srcs = glob(["vendor/proc-macro2-1.0.28/src/**"]), - visibility = ["PUBLIC"], + build_script = "vendor/proc-macro2-1.0.28/build.rs", features = [ "proc-macro", "span-locations", ], - rustc_flags = [ - "--cfg=span_locations", - "--cfg=use_proc_macro", - "--cfg=wrap_proc_macro", - ], + visibility = ["PUBLIC"], deps = [":unicode-xid"], ) rust_library( name = "quote", srcs = glob(["vendor/quote-1.0.9/src/**"]), - visibility = ["PUBLIC"], features = ["proc-macro"], + visibility = ["PUBLIC"], deps = [":proc-macro2"], ) @@ -72,8 +70,8 @@ rust_library( rust_library( name = "syn", - srcs = glob(["vendor/syn-1.0.74/src/**"]), - visibility = ["PUBLIC"], + srcs = glob(["vendor/syn-1.0.75/src/**"]), + build_script = "vendor/syn-1.0.75/build.rs", features = [ "clone-impls", "derive", @@ -82,6 +80,7 @@ rust_library( "printing", "proc-macro", ], + visibility = ["PUBLIC"], deps = [ ":proc-macro2", ":quote", diff --git a/third-party/BUILD b/third-party/BUILD index 57c584a7..1760b707 100644 --- a/third-party/BUILD +++ b/third-party/BUILD @@ -1,12 +1,13 @@ load( - "//tools/bazel:rust.bzl", + "//tools/bazel:third_party.bzl", + cargo_build_script = "third_party_cargo_build_script", glob = "third_party_glob", rust_library = "third_party_rust_library", ) rust_library( name = "bitflags", - srcs = glob(["vendor/bitflags-1.2.1/src/**"]), + srcs = glob(["vendor/bitflags-1.3.2/src/**"]), ) rust_library( @@ -50,13 +51,21 @@ rust_library( "proc-macro", "span-locations", ], - rustc_flags = [ - "--cfg=span_locations", - "--cfg=use_proc_macro", - "--cfg=wrap_proc_macro", - ], visibility = ["//visibility:public"], - deps = [":unicode-xid"], + deps = [ + ":proc-macro2@build", + ":unicode-xid", + ], +) + +cargo_build_script( + name = "proc-macro2@build", + srcs = ["vendor/proc-macro2-1.0.28/build.rs"], + crate_features = [ + "proc-macro", + "span-locations", + ], + crate_name = "build", ) rust_library( @@ -76,7 +85,7 @@ rust_library( rust_library( name = "syn", - srcs = glob(["vendor/syn-1.0.74/src/**"]), + srcs = glob(["vendor/syn-1.0.75/src/**"]), crate_features = [ "clone-impls", "derive", @@ -89,10 +98,25 @@ rust_library( deps = [ ":proc-macro2", ":quote", + ":syn@build", ":unicode-xid", ], ) +cargo_build_script( + name = "syn@build", + srcs = ["vendor/syn-1.0.75/build.rs"], + crate_features = [ + "clone-impls", + "derive", + "full", + "parsing", + "printing", + "proc-macro", + ], + crate_name = "build", +) + rust_library( name = "termcolor", srcs = glob(["vendor/termcolor-1.1.2/src/**"]), diff --git a/third-party/Cargo.lock b/third-party/Cargo.lock index 78108c01..0a3d543d 100644 --- a/third-party/Cargo.lock +++ b/third-party/Cargo.lock @@ -36,9 +36,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cc" @@ -100,7 +100,7 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.51" +version = "1.0.54" dependencies = [ "cc", "cxx-build", @@ -115,7 +115,7 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.51" +version = "1.0.54" dependencies = [ "cc", "codespan-reporting", @@ -130,7 +130,7 @@ dependencies = [ [[package]] name = "cxx-gen" -version = "0.7.51" +version = "0.7.54" dependencies = [ "cc", "codespan-reporting", @@ -150,7 +150,7 @@ dependencies = [ [[package]] name = "cxxbridge-cmd" -version = "1.0.51" +version = "1.0.54" dependencies = [ "clap", "codespan-reporting", @@ -161,11 +161,11 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.51" +version = "1.0.54" [[package]] name = "cxxbridge-macro" -version = "1.0.51" +version = "1.0.54" dependencies = [ "clang-ast", "cxx", @@ -221,15 +221,15 @@ dependencies = [ [[package]] name = "itoa" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "jobserver" -version = "0.1.22" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "972f5ae5d1cb9c6ae417789196c803205313edde988685da5e3aae0827b9e7fd" +checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa" dependencies = [ "libc", ] @@ -242,9 +242,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.98" +version = "0.2.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" +checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21" [[package]] name = "link-cplusplus" @@ -319,18 +319,18 @@ checksum = "7e114536316b51a5aa7a0e59fc49661fd263c5507dd08bd28de052e57626ce69" [[package]] name = "serde" -version = "1.0.127" +version = "1.0.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f03b9878abf6d14e6779d3f24f07b2cfa90352cfec4acc5aab8f1ac7f146fae8" +checksum = "d1f72836d2aa753853178eda473a3b9d8e4eefdaf20523b919677e6de489f8f1" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.127" +version = "1.0.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a024926d3432516606328597e0f224a51355a493b49fdd67e9209187cbe55ecc" +checksum = "e57ae87ad533d9a56427558b516d0adac283614e347abf85b0dc0cbbf0a249f3" dependencies = [ "proc-macro2", "quote", @@ -356,9 +356,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "syn" -version = "1.0.74" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" +checksum = "b7f58f7e8eaa0009c5fec437aabf511bd9933e4b2d7407bd05273c01a8906ea7" dependencies = [ "proc-macro2", "quote", @@ -394,9 +394,9 @@ dependencies = [ [[package]] name = "trybuild" -version = "1.0.43" +version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c02c413315329fc96167f922b46fd0caa3a43f4697b7a7896b183c7142635832" +checksum = "5bdaf2a1d317f3d58b44b31c7f6436b9b9acafe7bddfeace50897c2b804d7792" dependencies = [ "dissimilar", "glob", diff --git a/tools/bazel/rust.bzl b/tools/bazel/rust.bzl deleted file mode 100644 index 9f71923a..00000000 --- a/tools/bazel/rust.bzl +++ /dev/null @@ -1,29 +0,0 @@ -"""A module wrapping the core rules of `rules_rust`""" - -load( - "@rules_rust//rust:rust.bzl", - _rust_binary = "rust_binary", - _rust_library = "rust_library", - _rust_test = "rust_test", -) -load("@third-party//:vendor.bzl", "vendored") - -def third_party_glob(include): - return vendored and native.glob(include) - -def rust_binary(edition = "2018", **kwargs): - _rust_binary(edition = edition, **kwargs) - -def third_party_rust_binary(rustc_flags = [], **kwargs): - rustc_flags = rustc_flags + ["--cap-lints=allow"] - rust_binary(rustc_flags = rustc_flags, **kwargs) - -def rust_library(edition = "2018", **kwargs): - _rust_library(edition = edition, **kwargs) - -def third_party_rust_library(rustc_flags = [], **kwargs): - rustc_flags = rustc_flags + ["--cap-lints=allow"] - rust_library(rustc_flags = rustc_flags, **kwargs) - -def rust_test(edition = "2018", **kwargs): - _rust_test(edition = edition, **kwargs) diff --git a/tools/bazel/third_party.bzl b/tools/bazel/third_party.bzl new file mode 100644 index 00000000..7f51c466 --- /dev/null +++ b/tools/bazel/third_party.bzl @@ -0,0 +1,18 @@ +load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") +load("@rules_rust//rust:rust.bzl", "rust_binary", "rust_library") +load("@third-party//:vendor.bzl", "vendored") + +def third_party_glob(include): + return vendored and native.glob(include) + +def third_party_cargo_build_script(rustc_flags = [], **kwargs): + rustc_flags = rustc_flags + ["--cap-lints=allow"] + cargo_build_script(rustc_flags = rustc_flags, **kwargs) + +def third_party_rust_binary(rustc_flags = [], **kwargs): + rustc_flags = rustc_flags + ["--cap-lints=allow"] + rust_binary(rustc_flags = rustc_flags, **kwargs) + +def third_party_rust_library(rustc_flags = [], **kwargs): + rustc_flags = rustc_flags + ["--cap-lints=allow"] + rust_library(rustc_flags = rustc_flags, **kwargs) diff --git a/tools/bazel/vendor.bzl b/tools/bazel/vendor.bzl index d63ef92f..e51adb7b 100644 --- a/tools/bazel/vendor.bzl +++ b/tools/bazel/vendor.bzl @@ -2,7 +2,7 @@ of a crate in the current workspace. """ -load("@rules_rust//rust:repositories.bzl", "load_arbitrary_tool", "DEFAULT_RUST_VERSION") +load("@rules_rust//rust:repositories.bzl", "DEFAULT_RUST_VERSION", "load_arbitrary_tool") def _impl(repository_ctx): # Link cxx repository into @third-party. @@ -35,7 +35,7 @@ def _impl(repository_ctx): iso_date = repository_ctx.attr.cargo_iso_date, target_triple = target_triple, ) - + cmd = ["{}/bin/cargo".format(repository_ctx.path(".")), "vendor", "--versioned-dirs", "third-party/vendor"] result = repository_ctx.execute( cmd, diff --git a/tools/buck/rust_library.bzl b/tools/buck/rust_library.bzl new file mode 100644 index 00000000..67ec2ac7 --- /dev/null +++ b/tools/buck/rust_library.bzl @@ -0,0 +1,34 @@ +load("//tools/buck:genrule.bzl", "genrule") + +def rust_library( + name, + srcs, + features = [], + rustc_flags = [], + build_script = None, + **kwargs): + if build_script: + rust_binary( + name = "%s@build" % name, + srcs = srcs + [build_script], + crate = "build", + crate_root = build_script, + features = features, + rustc_flags = rustc_flags, + ) + + genrule( + name = "%s@cfg" % name, + out = "output", + cmd = "env RUSTC=rustc TARGET= $(exe :%s@build) | sed -n s/^cargo:rustc-cfg=/--cfg=/p > ${OUT}" % name, + ) + + rustc_flags = rustc_flags + ["@$(location :%s@cfg)" % name] + + native.rust_library( + name = name, + srcs = srcs, + features = features, + rustc_flags = rustc_flags, + **kwargs + ) |