Skip to content

Commit

Permalink
Auto merge of rust-lang#131903 - matthiaskrgr:rollup-bqqaah0, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 3 pull requests

Successful merges:

 - rust-lang#127462 (std: uefi: Add basic Env variables)
 - rust-lang#131537 (Fix range misleading field access)
 - rust-lang#131899 (Mark unexpected variant res suggestion as having placeholders)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Oct 18, 2024
2 parents e92993d + 4031762 commit 7879503
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 43 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn report_unexpected_variant_res(
}
}

err.multipart_suggestion_verbose(descr, suggestion, Applicability::MaybeIncorrect);
err.multipart_suggestion_verbose(descr, suggestion, Applicability::HasPlaceholders);
err
}
Res::Def(DefKind::Variant, _) if expr.is_none() => {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4011,6 +4011,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
let instead = res.is_some();
let suggestion = if let Some((start, end)) = this.diag_metadata.in_range
&& path[0].ident.span.lo() == end.span.lo()
&& !matches!(start.kind, ExprKind::Lit(_))
{
let mut sugg = ".";
let mut span = start.span.between(end.span);
Expand Down
23 changes: 22 additions & 1 deletion library/std/src/sys/pal/uefi/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! - More information about protocols can be found [here](https://edk2-docs.gitbook.io/edk-ii-uefi-driver-writer-s-guide/3_foundation/36_protocols_and_handles)

use r_efi::efi::{self, Guid};
use r_efi::protocols::{device_path, device_path_to_text};
use r_efi::protocols::{device_path, device_path_to_text, shell};

use crate::ffi::{OsStr, OsString};
use crate::io::{self, const_io_error};
Expand Down Expand Up @@ -424,3 +424,24 @@ pub(crate) fn os_string_to_raw(s: &OsStr) -> Option<Box<[r_efi::efi::Char16]>> {
let temp = s.encode_wide().chain(Some(0)).collect::<Box<[r_efi::efi::Char16]>>();
if temp[..temp.len() - 1].contains(&0) { None } else { Some(temp) }
}

pub(crate) fn open_shell() -> Option<NonNull<shell::Protocol>> {
static LAST_VALID_HANDLE: AtomicPtr<crate::ffi::c_void> =
AtomicPtr::new(crate::ptr::null_mut());

if let Some(handle) = NonNull::new(LAST_VALID_HANDLE.load(Ordering::Acquire)) {
if let Ok(protocol) = open_protocol::<shell::Protocol>(handle, shell::PROTOCOL_GUID) {
return Some(protocol);
}
}

let handles = locate_handles(shell::PROTOCOL_GUID).ok()?;
for handle in handles {
if let Ok(protocol) = open_protocol::<shell::Protocol>(handle, shell::PROTOCOL_GUID) {
LAST_VALID_HANDLE.store(handle.as_ptr(), Ordering::Release);
return Some(protocol);
}
}

None
}
144 changes: 103 additions & 41 deletions library/std/src/sys/pal/uefi/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn error_string(errno: RawOsError) -> String {
}

pub fn getcwd() -> io::Result<PathBuf> {
match uefi_shell::open_shell() {
match helpers::open_shell() {
Some(shell) => {
// SAFETY: path_ptr is managed by UEFI shell and should not be deallocated
let path_ptr = unsafe { ((*shell.as_ptr()).get_cur_dir)(crate::ptr::null_mut()) };
Expand All @@ -144,7 +144,7 @@ pub fn getcwd() -> io::Result<PathBuf> {
}

pub fn chdir(p: &path::Path) -> io::Result<()> {
let shell = uefi_shell::open_shell().ok_or(unsupported_err())?;
let shell = helpers::open_shell().ok_or(unsupported_err())?;

let mut p = helpers::os_string_to_raw(p.as_os_str())
.ok_or(io::const_io_error!(io::ErrorKind::InvalidData, "Invalid path"))?;
Expand Down Expand Up @@ -192,44 +192,58 @@ pub fn current_exe() -> io::Result<PathBuf> {
helpers::device_path_to_text(protocol).map(PathBuf::from)
}

pub struct Env(!);
pub struct EnvStrDebug<'a> {
iter: &'a [(OsString, OsString)],
}

impl fmt::Debug for EnvStrDebug<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut list = f.debug_list();
for (a, b) in self.iter {
list.entry(&(a.to_str().unwrap(), b.to_str().unwrap()));
}
list.finish()
}
}

pub struct Env(crate::vec::IntoIter<(OsString, OsString)>);

impl Env {
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
let Self(inner) = self;
match *inner {}
EnvStrDebug { iter: self.0.as_slice() }
}
}

impl Iterator for Env {
type Item = (OsString, OsString);

fn next(&mut self) -> Option<(OsString, OsString)> {
self.0
self.0.next()
}
}

impl fmt::Debug for Env {
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self(inner) = self;
match *inner {}
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

pub fn env() -> Env {
panic!("not supported on this platform")
let env = uefi_env::get_all().expect("not supported on this platform");
Env(env.into_iter())
}

pub fn getenv(_: &OsStr) -> Option<OsString> {
None
pub fn getenv(key: &OsStr) -> Option<OsString> {
uefi_env::get(key)
}

pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
pub unsafe fn setenv(key: &OsStr, val: &OsStr) -> io::Result<()> {
uefi_env::set(key, val)
}

pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
pub unsafe fn unsetenv(key: &OsStr) -> io::Result<()> {
uefi_env::unset(key)
}

pub fn temp_dir() -> PathBuf {
Expand Down Expand Up @@ -261,36 +275,84 @@ pub fn getpid() -> u32 {
panic!("no pids on this platform")
}

mod uefi_shell {
use r_efi::protocols::shell;

use super::super::helpers;
mod uefi_env {
use crate::ffi::{OsStr, OsString};
use crate::io;
use crate::os::uefi::ffi::OsStringExt;
use crate::ptr::NonNull;
use crate::sync::atomic::{AtomicPtr, Ordering};

pub fn open_shell() -> Option<NonNull<shell::Protocol>> {
static LAST_VALID_HANDLE: AtomicPtr<crate::ffi::c_void> =
AtomicPtr::new(crate::ptr::null_mut());

if let Some(handle) = NonNull::new(LAST_VALID_HANDLE.load(Ordering::Acquire)) {
if let Ok(protocol) = helpers::open_protocol::<shell::Protocol>(
handle,
r_efi::protocols::shell::PROTOCOL_GUID,
) {
return Some(protocol);
}
use crate::sys::{helpers, unsupported_err};

pub(crate) fn get(key: &OsStr) -> Option<OsString> {
let shell = helpers::open_shell()?;
let mut key_ptr = helpers::os_string_to_raw(key)?;
unsafe { get_raw(shell, key_ptr.as_mut_ptr()) }
}

pub(crate) fn set(key: &OsStr, val: &OsStr) -> io::Result<()> {
let mut key_ptr = helpers::os_string_to_raw(key)
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput, "Invalid Key"))?;
let mut val_ptr = helpers::os_string_to_raw(val)
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput, "Invalid Value"))?;
unsafe { set_raw(key_ptr.as_mut_ptr(), val_ptr.as_mut_ptr()) }
}

pub(crate) fn unset(key: &OsStr) -> io::Result<()> {
let mut key_ptr = helpers::os_string_to_raw(key)
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput, "Invalid Key"))?;
unsafe { set_raw(key_ptr.as_mut_ptr(), crate::ptr::null_mut()) }
}

pub(crate) fn get_all() -> io::Result<Vec<(OsString, OsString)>> {
let shell = helpers::open_shell().ok_or(unsupported_err())?;

let mut vars = Vec::new();
let val = unsafe { ((*shell.as_ptr()).get_env)(crate::ptr::null_mut()) };

if val.is_null() {
return Ok(vars);
}

let handles = helpers::locate_handles(shell::PROTOCOL_GUID).ok()?;
for handle in handles {
if let Ok(protocol) =
helpers::open_protocol::<shell::Protocol>(handle, shell::PROTOCOL_GUID)
{
LAST_VALID_HANDLE.store(handle.as_ptr(), Ordering::Release);
return Some(protocol);
let mut start = 0;

// UEFI Shell returns all keys seperated by NULL.
// End of string is denoted by two NULLs
for i in 0.. {
if unsafe { *val.add(i) } == 0 {
// Two NULL signal end of string
if i == start {
break;
}

let key = OsString::from_wide(unsafe {
crate::slice::from_raw_parts(val.add(start), i - start)
});
// SAFETY: val.add(start) is always NULL terminated
let val = unsafe { get_raw(shell, val.add(start)) }
.ok_or(io::const_io_error!(io::ErrorKind::InvalidInput, "Invalid Value"))?;

vars.push((key, val));
start = i + 1;
}
}

None
Ok(vars)
}

unsafe fn get_raw(
shell: NonNull<r_efi::efi::protocols::shell::Protocol>,
key_ptr: *mut r_efi::efi::Char16,
) -> Option<OsString> {
let val = unsafe { ((*shell.as_ptr()).get_env)(key_ptr) };
helpers::os_string_from_raw(val)
}

unsafe fn set_raw(
key_ptr: *mut r_efi::efi::Char16,
val_ptr: *mut r_efi::efi::Char16,
) -> io::Result<()> {
let shell = helpers::open_shell().ok_or(unsupported_err())?;
let r =
unsafe { ((*shell.as_ptr()).set_env)(key_ptr, val_ptr, r_efi::efi::Boolean::FALSE) };
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
}
}
8 changes: 8 additions & 0 deletions tests/ui/range/misleading-field-access-hint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Check if rustc still displays the misleading hint to write `.` instead of `..`
fn main() {
let width = 10;
// ...
for _ in 0..w {
//~^ ERROR cannot find value `w`
}
}
9 changes: 9 additions & 0 deletions tests/ui/range/misleading-field-access-hint.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0425]: cannot find value `w` in this scope
--> $DIR/misleading-field-access-hint.rs:5:17
|
LL | for _ in 0..w {
| ^ not found in this scope

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0425`.

0 comments on commit 7879503

Please sign in to comment.