Skip to content

Commit

Permalink
Fix unexpected_cfgs warnings. (#358)
Browse files Browse the repository at this point in the history
* Fix unexpected_cfgs warnings.

Fix warnings about cfgs that aren't declared, by declaring the cfgs we
do use in build.rs files. And fix a bug this turned up, around the usage
of unix_file_vectored_at.

* Fix an unexpected_cfg warning.

* Use `cargo:` instead of `cargo::` for broader compatibility.

* Fix `cfg(feature = "async_std")` in cap-fs-ext.

* Fix the test to be independent of the expected error message.
  • Loading branch information
sunfishcode authored Jul 2, 2024
1 parent 68cefe7 commit c3e03fd
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 50 deletions.
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ fn main() {
// https://doc.rust-lang.org/unstable-book/library-features/windows-file-type-ext.html
use_feature_or_nothing("windows_file_type_ext");

// Cfgs that users may set.
println!("cargo:rustc-check-cfg=cfg(racy_asserts)");

// Don't rerun this on changes other than build.rs, as we only depend on
// the rustc version.
println!("cargo:rerun-if-changed=build.rs");
Expand All @@ -26,6 +29,7 @@ fn use_feature_or_nothing(feature: &str) {
if has_feature(feature) {
use_feature(feature);
}
println!("cargo:rustc-check-cfg=cfg({})", feature);
}

fn use_feature(feature: &str) {
Expand Down
1 change: 1 addition & 0 deletions cap-fs-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ default = ["std"]
fs_utf8 = ["cap-std/fs_utf8", "camino"]
arf_strings = ["cap-std/arf_strings", "fs_utf8", "arf-strings"]
std = ["cap-std"]
# Temporarily disable cap-async-std.
#async_std = ["cap-async-std", "async-std", "io-lifetimes/async-std", "async-trait"]
#async_std_fs_utf8 = ["cap-async-std/fs_utf8", "camino"]
#async_std_arf_strings = ["cap-async-std/arf_strings", "async_std_fs_utf8", "arf-strings"]
Expand Down
1 change: 1 addition & 0 deletions cap-fs-ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn use_feature_or_nothing(feature: &str) {
if has_feature(feature) {
use_feature(feature);
}
println!("cargo:rustc-check-cfg=cfg({})", feature);
}

fn use_feature(feature: &str) {
Expand Down
4 changes: 2 additions & 2 deletions cap-fs-ext/src/dir_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,13 +1393,13 @@ impl AsyncDirExtUtf8 for cap_async_std::fs_utf8::Dir {

#[cfg(all(any(feature = "std", feature = "async_std"), feature = "fs_utf8"))]
#[cfg(not(feature = "arf_strings"))]
fn from_utf8<'a>(path: &'a Utf8Path) -> std::io::Result<&'a std::path::Path> {
fn from_utf8<'a>(path: &'a Utf8Path) -> io::Result<&'a std::path::Path> {
Ok(path.as_std_path())
}

#[cfg(all(any(feature = "std", feature = "async_std"), feature = "fs_utf8"))]
#[cfg(feature = "arf_strings")]
fn from_utf8<'a>(path: &'a Utf8Path) -> std::io::Result<std::path::PathBuf> {
fn from_utf8<'a>(path: &'a Utf8Path) -> io::Result<std::path::PathBuf> {
#[cfg(not(windows))]
let path = {
#[cfg(unix)]
Expand Down
3 changes: 3 additions & 0 deletions cap-fs-ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#![doc(
html_favicon_url = "https://raw.githubusercontent.com/bytecodealliance/cap-std/main/media/cap-std.ico"
)]
// Allow cfg(feature = "async_std") even though it isn't a feature. async_std
// support is temporarily disabled.
#![allow(unexpected_cfgs)]

mod dir_entry_ext;
mod dir_ext;
Expand Down
6 changes: 6 additions & 0 deletions cap-primitives/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ fn main() {
use_feature_or_nothing("io_error_more"); // https://github.com/rust-lang/rust/issues/86442
use_feature_or_nothing("io_error_uncategorized");

// Cfgs that users may set.
println!("cargo:rustc-check-cfg=cfg(racy_asserts)");
println!("cargo:rustc-check-cfg=cfg(emulate_second_only_system)");
println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)");

// Don't rerun this on changes other than build.rs, as we only depend on
// the rustc version.
println!("cargo:rerun-if-changed=build.rs");
Expand All @@ -18,6 +23,7 @@ fn use_feature_or_nothing(feature: &str) {
if has_feature(feature) {
use_feature(feature);
}
println!("cargo:rustc-check-cfg=cfg({})", feature);
}

fn use_feature(feature: &str) {
Expand Down
62 changes: 39 additions & 23 deletions cap-primitives/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ pub trait FileExt {
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;

/// Like `read_at`, except that it reads into a slice of buffers.
#[cfg(feature = "unix_file_vectored_at")]
fn read_vectored_at(
&self,
bufs: &mut [std::io::IoSliceMut<'_>],
offset: u64,
) -> io::Result<usize> {
io::default_read_vectored(|b| self.read_at(b, offset), bufs)
#[cfg(unix_file_vectored_at)]
fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
default_read_vectored(|b| self.read_at(b, offset), bufs)
}

/// Reads the exact number of bytes required to fill `buf` from the given offset.
Expand Down Expand Up @@ -44,9 +40,9 @@ pub trait FileExt {
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;

/// Like `write_at`, except that it writes from a slice of buffers.
#[cfg(feature = "unix_file_vectored_at")]
fn write_vectored_at(&self, bufs: &[std::io::IoSlice<'_>], offset: u64) -> io::Result<usize> {
io::default_write_vectored(|b| self.write_at(b, offset), bufs)
#[cfg(unix_file_vectored_at)]
fn write_vectored_at(&self, bufs: &[io::IoSlice<'_>], offset: u64) -> io::Result<usize> {
default_write_vectored(|b| self.write_at(b, offset), bufs)
}

/// Attempts to write an entire buffer starting from a given offset.
Expand All @@ -71,21 +67,41 @@ pub trait FileExt {
}
}

#[cfg(unix_file_vectored_at)]
fn default_read_vectored<F>(read: F, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize>
where
F: FnOnce(&mut [u8]) -> io::Result<usize>,
{
let buf = bufs
.iter_mut()
.find(|b| !b.is_empty())
.map_or(&mut [][..], |b| &mut **b);
read(buf)
}

#[cfg(unix_file_vectored_at)]
fn default_write_vectored<F>(write: F, bufs: &[io::IoSlice<'_>]) -> io::Result<usize>
where
F: FnOnce(&[u8]) -> io::Result<usize>,
{
let buf = bufs
.iter()
.find(|b| !b.is_empty())
.map_or(&[][..], |b| &**b);
write(buf)
}

/// WASI-specific extensions to [`fs::File`].
#[cfg(target_os = "wasi")]
pub trait FileExt {
/// Reads a number of bytes starting from a given offset.
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
let bufs = &mut [std::io::IoSliceMut::new(buf)];
let bufs = &mut [io::IoSliceMut::new(buf)];
self.read_vectored_at(bufs, offset)
}

/// Reads a number of bytes starting from a given offset.
fn read_vectored_at(
&self,
bufs: &mut [std::io::IoSliceMut<'_>],
offset: u64,
) -> io::Result<usize>;
fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut<'_>], offset: u64) -> io::Result<usize>;

/// Reads the exact number of byte required to fill `buf` from the given offset.
fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
Expand Down Expand Up @@ -113,12 +129,12 @@ pub trait FileExt {

/// Writes a number of bytes starting from a given offset.
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
let bufs = &[std::io::IoSlice::new(buf)];
let bufs = &[io::IoSlice::new(buf)];
self.write_vectored_at(bufs, offset)
}

/// Writes a number of bytes starting from a given offset.
fn write_vectored_at(&self, bufs: &[std::io::IoSlice<'_>], offset: u64) -> io::Result<usize>;
fn write_vectored_at(&self, bufs: &[io::IoSlice<'_>], offset: u64) -> io::Result<usize>;

/// Attempts to write an entire buffer starting from a given offset.
fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
Expand All @@ -145,19 +161,19 @@ pub trait FileExt {
fn tell(&self) -> io::Result<u64>;

/// Adjust the flags associated with this file.
fn fdstat_set_flags(&self, flags: u16) -> std::io::Result<()>;
fn fdstat_set_flags(&self, flags: u16) -> io::Result<()>;

/// Adjust the rights associated with this file.
fn fdstat_set_rights(&self, rights: u64, inheriting: u64) -> std::io::Result<()>;
fn fdstat_set_rights(&self, rights: u64, inheriting: u64) -> io::Result<()>;

/// Provide file advisory information on a file descriptor.
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::io::Result<()>;
fn advise(&self, offset: u64, len: u64, advice: u8) -> io::Result<()>;

/// Force the allocation of space in a file.
fn allocate(&self, offset: u64, len: u64) -> std::io::Result<()>;
fn allocate(&self, offset: u64, len: u64) -> io::Result<()>;

/// Create a directory.
fn create_directory<P: AsRef<std::path::Path>>(&self, dir: P) -> std::io::Result<()>;
fn create_directory<P: AsRef<std::path::Path>>(&self, dir: P) -> io::Result<()>;

/// Read the contents of a symbolic link.
fn read_link<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<std::path::PathBuf>;
Expand Down
2 changes: 1 addition & 1 deletion cap-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition = "2021"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg=doc_cfg"]
rustdoc-args = ["--cfg=docsrs"]

[dependencies]
arf-strings = { version = "0.7.0", optional = true }
Expand Down
5 changes: 5 additions & 0 deletions cap-std/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::io::Write;
fn main() {
use_feature_or_nothing("can_vector"); // https://github.com/rust-lang/rust/issues/69941
use_feature_or_nothing("write_all_vectored"); // https://github.com/rust-lang/rust/issues/70436
use_feature_or_nothing("windows_file_type_ext");

// Cfgs that users may set.
println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)");

// Don't rerun this on changes other than build.rs, as we only depend on
// the rustc version.
Expand All @@ -14,6 +18,7 @@ fn use_feature_or_nothing(feature: &str) {
if has_feature(feature) {
use_feature(feature);
}
println!("cargo:rustc-check-cfg=cfg({})", feature);
}

fn use_feature(feature: &str) {
Expand Down
20 changes: 10 additions & 10 deletions cap-std/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,12 @@ impl crate::fs::FileExt for File {
}

#[inline]
fn tell(&self) -> std::result::Result<u64, std::io::Error> {
fn tell(&self) -> std::result::Result<u64, io::Error> {
std::os::wasi::fs::FileExt::tell(&self.std)
}

#[inline]
fn fdstat_set_flags(&self, flags: u16) -> std::result::Result<(), std::io::Error> {
fn fdstat_set_flags(&self, flags: u16) -> std::result::Result<(), io::Error> {
std::os::wasi::fs::FileExt::fdstat_set_flags(&self.std, flags)
}

Expand All @@ -538,30 +538,30 @@ impl crate::fs::FileExt for File {
&self,
rights: u64,
inheriting: u64,
) -> std::result::Result<(), std::io::Error> {
) -> std::result::Result<(), io::Error> {
std::os::wasi::fs::FileExt::fdstat_set_rights(&self.std, rights, inheriting)
}

#[inline]
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::result::Result<(), std::io::Error> {
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::result::Result<(), io::Error> {
std::os::wasi::fs::FileExt::advise(&self.std, offset, len, advice)
}

#[inline]
fn allocate(&self, offset: u64, len: u64) -> std::result::Result<(), std::io::Error> {
fn allocate(&self, offset: u64, len: u64) -> std::result::Result<(), io::Error> {
std::os::wasi::fs::FileExt::allocate(&self.std, offset, len)
}

#[inline]
fn create_directory<P: AsRef<Path>>(&self, dir: P) -> std::result::Result<(), std::io::Error> {
fn create_directory<P: AsRef<Path>>(&self, dir: P) -> std::result::Result<(), io::Error> {
std::os::wasi::fs::FileExt::create_directory(&self.std, dir)
}

#[inline]
fn read_link<P: AsRef<Path>>(
&self,
path: P,
) -> std::result::Result<std::path::PathBuf, std::io::Error> {
) -> std::result::Result<std::path::PathBuf, io::Error> {
std::os::wasi::fs::FileExt::read_link(&self.std, path)
}

Expand All @@ -570,17 +570,17 @@ impl crate::fs::FileExt for File {
&self,
lookup_flags: u32,
path: P,
) -> std::result::Result<std::fs::Metadata, std::io::Error> {
) -> std::result::Result<std::fs::Metadata, io::Error> {
std::os::wasi::fs::FileExt::metadata_at(&self.std, lookup_flags, path)
}

#[inline]
fn remove_file<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), std::io::Error> {
fn remove_file<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), io::Error> {
std::os::wasi::fs::FileExt::remove_file(&self.std, path)
}

#[inline]
fn remove_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), std::io::Error> {
fn remove_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), io::Error> {
std::os::wasi::fs::FileExt::remove_directory(&self.std, path)
}
}
Expand Down
20 changes: 10 additions & 10 deletions cap-std/src/fs_utf8/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,12 @@ impl crate::fs::FileExt for File {
}

#[inline]
fn tell(&self) -> std::result::Result<u64, std::io::Error> {
fn tell(&self) -> std::result::Result<u64, io::Error> {
self.cap_std.tell()
}

#[inline]
fn fdstat_set_flags(&self, flags: u16) -> std::result::Result<(), std::io::Error> {
fn fdstat_set_flags(&self, flags: u16) -> std::result::Result<(), io::Error> {
self.cap_std.fdstat_set_flags(flags)
}

Expand All @@ -536,22 +536,22 @@ impl crate::fs::FileExt for File {
&self,
rights: u64,
inheriting: u64,
) -> std::result::Result<(), std::io::Error> {
) -> std::result::Result<(), io::Error> {
self.cap_std.fdstat_set_rights(rights, inheriting)
}

#[inline]
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::result::Result<(), std::io::Error> {
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::result::Result<(), io::Error> {
self.cap_std.advise(offset, len, advice)
}

#[inline]
fn allocate(&self, offset: u64, len: u64) -> std::result::Result<(), std::io::Error> {
fn allocate(&self, offset: u64, len: u64) -> std::result::Result<(), io::Error> {
self.cap_std.allocate(offset, len)
}

#[inline]
fn create_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), std::io::Error> {
fn create_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), io::Error> {
let path = path.as_ref();
self.cap_std.create_directory(path)
}
Expand All @@ -560,7 +560,7 @@ impl crate::fs::FileExt for File {
fn read_link<P: AsRef<Path>>(
&self,
path: P,
) -> std::result::Result<std::path::PathBuf, std::io::Error> {
) -> std::result::Result<std::path::PathBuf, io::Error> {
let path = path.as_ref();
self.cap_std.read_link(path)
}
Expand All @@ -570,19 +570,19 @@ impl crate::fs::FileExt for File {
&self,
lookup_flags: u32,
path: P,
) -> std::result::Result<std::fs::Metadata, std::io::Error> {
) -> std::result::Result<std::fs::Metadata, io::Error> {
let path = path.as_ref();
self.cap_std.metadata_at(lookup_flags, path)
}

#[inline]
fn remove_file<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), std::io::Error> {
fn remove_file<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), io::Error> {
let path = path.as_ref();
self.cap_std.remove_file(path)
}

#[inline]
fn remove_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), std::io::Error> {
fn remove_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), io::Error> {
let path = path.as_ref();
self.cap_std.remove_directory(path)
}
Expand Down
2 changes: 1 addition & 1 deletion cap-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! [`Pool`]: net::Pool
#![deny(missing_docs)]
#![cfg_attr(doc_cfg, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
#![cfg_attr(can_vector, feature(can_vector))]
#![cfg_attr(write_all_vectored, feature(write_all_vectored))]
Expand Down
7 changes: 4 additions & 3 deletions tests/cap-basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ fn symlink_loop_from_rename() {
check!(tmpdir.open("link"));
}

#[cfg(linux)]
#[cfg(target_os = "linux")]
#[test]
fn proc_self_fd() {
let fd = check!(File::open("/proc/self/fd"));
let fd = check!(std::fs::File::open("/proc/self/fd"));
let dir = cap_std::fs::Dir::from_std_file(fd);
error!(dir.open("0"), "No such file");
// This should fail with "too many levels of symbolic links".
dir.open("0").unwrap_err();
}

0 comments on commit c3e03fd

Please sign in to comment.