diff options
Diffstat (limited to 'gcc/rust/checks/errors')
28 files changed, 534 insertions, 693 deletions
diff --git a/gcc/rust/checks/errors/borrowck/ffi-polonius/.cargo/config.toml b/gcc/rust/checks/errors/borrowck/ffi-polonius/.cargo/config.toml deleted file mode 100644 index 0236928..0000000 --- a/gcc/rust/checks/errors/borrowck/ffi-polonius/.cargo/config.toml +++ /dev/null @@ -1,5 +0,0 @@ -[source.crates-io] -replace-with = "vendored-sources" - -[source.vendored-sources] -directory = "vendor" diff --git a/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock b/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock index f7cbd41..1b223b6 100644 --- a/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock +++ b/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock @@ -1,12 +1,8 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 - [[package]] name = "datafrog" version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0afaad2b26fa326569eb264b1363e8ae3357618c43982b3f285f0774ce76b69" [[package]] name = "ffi-polonius" @@ -18,14 +14,10 @@ dependencies = [ [[package]] name = "log" version = "0.4.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "polonius-engine" version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4e8e505342045d397d0b6674dcb82d6faf5cf40484d30eeb88fc82ef14e903f" dependencies = [ "datafrog", "log", @@ -35,5 +27,3 @@ dependencies = [ [[package]] name = "rustc-hash" version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" diff --git a/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.toml b/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.toml index 71315c3..3bc8e3f 100644 --- a/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.toml +++ b/gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.toml @@ -1,11 +1,17 @@ [package] name = "ffi-polonius" version = "0.1.0" -edition = "2021" +edition = "2018" license = "GPL-3" [lib] crate-type = ["staticlib"] [dependencies] -polonius-engine = "0.13.0"
\ No newline at end of file +polonius-engine = "0.13.0" + +[patch.crates-io] +log = { path = "vendor/log" } +datafrog = { path = "vendor/datafrog" } +polonius-engine = { path = "vendor/polonius-engine" } +rustc-hash = { path = "vendor/rustc-hash" } diff --git a/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/Cargo.toml b/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/Cargo.toml index 313a005..a199e31 100644 --- a/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/Cargo.toml +++ b/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/Cargo.toml @@ -10,7 +10,7 @@ # See Cargo.toml.orig for the original contents. [package] -edition = "2021" +edition = "2018" rust-version = "1.60.0" name = "log" version = "0.4.22" diff --git a/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/src/lib.rs b/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/src/lib.rs index 6b43a9a..603bbac 100644 --- a/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/src/lib.rs +++ b/gcc/rust/checks/errors/borrowck/ffi-polonius/vendor/log/src/lib.rs @@ -397,20 +397,13 @@ mod serde; #[cfg(feature = "kv")] pub mod kv; -#[cfg(target_has_atomic = "ptr")] -use std::sync::atomic::{AtomicUsize, Ordering}; - -#[cfg(not(target_has_atomic = "ptr"))] use std::cell::Cell; -#[cfg(not(target_has_atomic = "ptr"))] use std::sync::atomic::Ordering; -#[cfg(not(target_has_atomic = "ptr"))] struct AtomicUsize { v: Cell<usize>, } -#[cfg(not(target_has_atomic = "ptr"))] impl AtomicUsize { const fn new(v: usize) -> AtomicUsize { AtomicUsize { v: Cell::new(v) } @@ -423,26 +416,10 @@ impl AtomicUsize { fn store(&self, val: usize, _order: Ordering) { self.v.set(val) } - - #[cfg(target_has_atomic = "ptr")] - fn compare_exchange( - &self, - current: usize, - new: usize, - _success: Ordering, - _failure: Ordering, - ) -> Result<usize, usize> { - let prev = self.v.get(); - if current == prev { - self.v.set(new); - } - Ok(prev) - } } // Any platform without atomics is unlikely to have multiple cores, so // writing via Cell will not be a race condition. -#[cfg(not(target_has_atomic = "ptr"))] unsafe impl Sync for AtomicUsize {} // The LOGGER static holds a pointer to the global logger. It is protected by @@ -1258,17 +1235,6 @@ where } } -/// Sets the global maximum log level. -/// -/// Generally, this should only be called by the active logging implementation. -/// -/// Note that `Trace` is the maximum level, because it provides the maximum amount of detail in the emitted logs. -#[inline] -#[cfg(target_has_atomic = "ptr")] -pub fn set_max_level(level: LevelFilter) { - MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed); -} - /// A thread-unsafe version of [`set_max_level`]. /// /// This function is available on all platforms, even those that do not have @@ -1320,110 +1286,6 @@ pub fn max_level() -> LevelFilter { unsafe { mem::transmute(MAX_LOG_LEVEL_FILTER.load(Ordering::Relaxed)) } } -/// Sets the global logger to a `Box<Log>`. -/// -/// This is a simple convenience wrapper over `set_logger`, which takes a -/// `Box<Log>` rather than a `&'static Log`. See the documentation for -/// [`set_logger`] for more details. -/// -/// Requires the `std` feature. -/// -/// # Errors -/// -/// An error is returned if a logger has already been set. -/// -/// [`set_logger`]: fn.set_logger.html -#[cfg(all(feature = "std", target_has_atomic = "ptr"))] -pub fn set_boxed_logger(logger: Box<dyn Log>) -> Result<(), SetLoggerError> { - set_logger_inner(|| Box::leak(logger)) -} - -/// Sets the global logger to a `&'static Log`. -/// -/// This function may only be called once in the lifetime of a program. Any log -/// events that occur before the call to `set_logger` completes will be ignored. -/// -/// This function does not typically need to be called manually. Logger -/// implementations should provide an initialization method that installs the -/// logger internally. -/// -/// # Availability -/// -/// This method is available even when the `std` feature is disabled. However, -/// it is currently unavailable on `thumbv6` targets, which lack support for -/// some atomic operations which are used by this function. Even on those -/// targets, [`set_logger_racy`] will be available. -/// -/// # Errors -/// -/// An error is returned if a logger has already been set. -/// -/// # Examples -/// -/// ``` -/// use log::{error, info, warn, Record, Level, Metadata, LevelFilter}; -/// -/// static MY_LOGGER: MyLogger = MyLogger; -/// -/// struct MyLogger; -/// -/// impl log::Log for MyLogger { -/// fn enabled(&self, metadata: &Metadata) -> bool { -/// metadata.level() <= Level::Info -/// } -/// -/// fn log(&self, record: &Record) { -/// if self.enabled(record.metadata()) { -/// println!("{} - {}", record.level(), record.args()); -/// } -/// } -/// fn flush(&self) {} -/// } -/// -/// # fn main(){ -/// log::set_logger(&MY_LOGGER).unwrap(); -/// log::set_max_level(LevelFilter::Info); -/// -/// info!("hello log"); -/// warn!("warning"); -/// error!("oops"); -/// # } -/// ``` -/// -/// [`set_logger_racy`]: fn.set_logger_racy.html -#[cfg(target_has_atomic = "ptr")] -pub fn set_logger(logger: &'static dyn Log) -> Result<(), SetLoggerError> { - set_logger_inner(|| logger) -} - -#[cfg(target_has_atomic = "ptr")] -fn set_logger_inner<F>(make_logger: F) -> Result<(), SetLoggerError> -where - F: FnOnce() -> &'static dyn Log, -{ - match STATE.compare_exchange( - UNINITIALIZED, - INITIALIZING, - Ordering::Acquire, - Ordering::Relaxed, - ) { - Ok(UNINITIALIZED) => { - unsafe { - LOGGER = make_logger(); - } - STATE.store(INITIALIZED, Ordering::Release); - Ok(()) - } - Err(INITIALIZING) => { - while STATE.load(Ordering::Relaxed) == INITIALIZING { - std::hint::spin_loop(); - } - Err(SetLoggerError(())) - } - _ => Err(SetLoggerError(())), - } -} - /// A thread-unsafe version of [`set_logger`]. /// /// This function is available on all platforms, even those that do not have diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc index 1713bf6..d6acc6a 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc @@ -135,7 +135,7 @@ ExprStmtBuilder::visit (HIR::LiteralExpr &expr) void ExprStmtBuilder::visit (HIR::BorrowExpr &expr) { - auto operand = visit_expr (*expr.get_expr ()); + auto operand = visit_expr (expr.get_expr ()); if (ctx.place_db[operand].is_constant ()) { // Cannot borrow a constant, must create a temporary copy. @@ -150,7 +150,7 @@ ExprStmtBuilder::visit (HIR::BorrowExpr &expr) void ExprStmtBuilder::visit (HIR::DereferenceExpr &expr) { - auto operand = visit_expr (*expr.get_expr ()); + auto operand = visit_expr (expr.get_expr ()); return_place (ctx.place_db.lookup_or_add_path (Place::DEREF, lookup_type (expr), operand), expr.get_locus ()); @@ -166,30 +166,31 @@ ExprStmtBuilder::visit (HIR::ErrorPropagationExpr &expr) void ExprStmtBuilder::visit (HIR::NegationExpr &expr) { - PlaceId operand = visit_expr (*expr.get_expr ()); - return_expr (new Operator<1> ({move_place (operand, expr.get_locus ())}), + PlaceId operand = visit_expr (expr.get_expr ()); + return_expr (new Operator<1> ( + {move_place (operand, expr.get_expr ().get_locus ())}), lookup_type (expr), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::ArithmeticOrLogicalExpr &expr) { - PlaceId lhs = visit_expr (*expr.get_lhs ()); - PlaceId rhs = visit_expr (*expr.get_rhs ()); + PlaceId lhs = visit_expr (expr.get_lhs ()); + PlaceId rhs = visit_expr (expr.get_rhs ()); return_expr (new Operator<2> ( - {move_place (lhs, expr.get_lhs ()->get_locus ()), - move_place (rhs, expr.get_rhs ()->get_locus ())}), + {move_place (lhs, expr.get_lhs ().get_locus ()), + move_place (rhs, expr.get_rhs ().get_locus ())}), lookup_type (expr), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::ComparisonExpr &expr) { - PlaceId lhs = visit_expr (*expr.get_lhs ()); - PlaceId rhs = visit_expr (*expr.get_rhs ()); + PlaceId lhs = visit_expr (expr.get_lhs ()); + PlaceId rhs = visit_expr (expr.get_rhs ()); return_expr (new Operator<2> ( - {move_place (lhs, expr.get_lhs ()->get_locus ()), - move_place (rhs, expr.get_rhs ()->get_locus ())}), + {move_place (lhs, expr.get_lhs ().get_locus ()), + move_place (rhs, expr.get_rhs ().get_locus ())}), lookup_type (expr), expr.get_locus ()); } @@ -205,7 +206,7 @@ ExprStmtBuilder::visit (HIR::LazyBooleanExpr &expr) void ExprStmtBuilder::visit (HIR::TypeCastExpr &expr) { - auto operand = visit_expr (*expr.get_expr ()); + auto operand = visit_expr (expr.get_expr ()); return_expr (new Operator<1> ({operand}), lookup_type (expr), expr.get_locus ()); } @@ -213,8 +214,8 @@ ExprStmtBuilder::visit (HIR::TypeCastExpr &expr) void ExprStmtBuilder::visit (HIR::AssignmentExpr &expr) { - auto lhs = visit_expr (*expr.get_lhs ()); - auto rhs = visit_expr (*expr.get_rhs ()); + auto lhs = visit_expr (expr.get_lhs ()); + auto rhs = visit_expr (expr.get_rhs ()); push_assignment (lhs, rhs, expr.get_locus ()); translated = INVALID_PLACE; } @@ -222,25 +223,25 @@ ExprStmtBuilder::visit (HIR::AssignmentExpr &expr) void ExprStmtBuilder::visit (HIR::CompoundAssignmentExpr &expr) { - auto lhs = visit_expr (*expr.get_lhs ()); - auto rhs = visit_expr (*expr.get_rhs ()); + auto lhs = visit_expr (expr.get_lhs ()); + auto rhs = visit_expr (expr.get_rhs ()); push_assignment (lhs, new Operator<2> ({lhs, rhs}), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::GroupedExpr &expr) { - return_place (visit_expr (*expr.get_expr_in_parens ()), expr.get_locus ()); + return_place (visit_expr (expr.get_expr_in_parens ()), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::ArrayExpr &expr) { auto &elems = expr.get_internal_elements (); - switch (elems->get_array_expr_type ()) + switch (elems.get_array_expr_type ()) { case HIR::ArrayElems::VALUES: { - auto &elem_vals = (static_cast<HIR::ArrayElemsValues &> (*elems)); + auto &elem_vals = (static_cast<HIR::ArrayElemsValues &> (elems)); auto init_values = visit_list (elem_vals.get_values ()); // collect locations std::vector<location_t> value_locations; @@ -254,8 +255,8 @@ ExprStmtBuilder::visit (HIR::ArrayExpr &expr) break; } case HIR::ArrayElems::COPIED: { - auto &elem_copied = (static_cast<HIR::ArrayElemsCopied &> (*elems)); - auto init = visit_expr (*elem_copied.get_elem_to_copy ()); + auto &elem_copied = (static_cast<HIR::ArrayElemsCopied &> (elems)); + auto init = visit_expr (elem_copied.get_elem_to_copy ()); return_expr (new InitializerExpr ({init}), lookup_type (expr), expr.get_locus ()); break; @@ -266,8 +267,8 @@ ExprStmtBuilder::visit (HIR::ArrayExpr &expr) void ExprStmtBuilder::visit (HIR::ArrayIndexExpr &expr) { - auto lhs = visit_expr (*expr.get_array_expr ()); - auto rhs = visit_expr (*expr.get_index_expr ()); + auto lhs = visit_expr (expr.get_array_expr ()); + auto rhs = visit_expr (expr.get_index_expr ()); // The index is not tracked in BIR. std::ignore = rhs; return_place (ctx.place_db.lookup_or_add_path (Place::INDEX, @@ -286,7 +287,7 @@ ExprStmtBuilder::visit (HIR::TupleExpr &expr) void ExprStmtBuilder::visit (HIR::TupleIndexExpr &expr) { - auto tuple = visit_expr (*expr.get_tuple_expr ()); + auto tuple = visit_expr (expr.get_tuple_expr ()); return_place (ctx.place_db.lookup_or_add_path (Place::FIELD, lookup_type (expr), tuple, expr.get_tuple_index ()), @@ -296,7 +297,7 @@ ExprStmtBuilder::visit (HIR::TupleIndexExpr &expr) void ExprStmtBuilder::visit (HIR::CallExpr &expr) { - PlaceId fn = visit_expr (*expr.get_fnexpr ()); + PlaceId fn = visit_expr (expr.get_fnexpr ()); std::vector<PlaceId> arguments = visit_list (expr.get_arguments ()); const auto fn_type @@ -330,7 +331,7 @@ ExprStmtBuilder::visit (HIR::MethodCallExpr &expr) void ExprStmtBuilder::visit (HIR::FieldAccessExpr &expr) { - auto receiver = visit_expr (*expr.get_receiver_expr ()); + auto receiver = visit_expr (expr.get_receiver_expr ()); auto type = autoderef (receiver); rust_assert (type->get_kind () == TyTy::ADT); auto adt = type->as<TyTy::ADTType> (); @@ -383,7 +384,7 @@ ExprStmtBuilder::visit (HIR::BlockExpr &block) if (block.has_expr () && !unreachable) { push_assignment (block_ctx.label_var, - visit_expr (*block.get_final_expr ()), + visit_expr (block.get_final_expr ()), block.get_start_locus ()); } if (!ctx.get_current_bb ().is_terminated ()) @@ -397,9 +398,9 @@ ExprStmtBuilder::visit (HIR::BlockExpr &block) } else if (block.has_expr () && !unreachable) { - return_place (visit_expr (*block.get_final_expr (), + return_place (visit_expr (block.get_final_expr (), take_or_create_return_place ( - lookup_type (*block.get_final_expr ()))), + lookup_type (block.get_final_expr ()))), block.get_start_locus ()); } @@ -426,7 +427,7 @@ ExprStmtBuilder::visit (HIR::BreakExpr &brk) LoopAndLabelCtx info = brk.has_label () ? get_label_ctx (brk.get_label ()) : get_unnamed_loop_ctx (); if (brk.has_break_expr ()) - push_assignment (info.label_var, visit_expr (*brk.get_expr ()), + push_assignment (info.label_var, visit_expr (brk.get_expr ()), brk.get_locus ()); start_new_consecutive_bb (); @@ -438,8 +439,8 @@ ExprStmtBuilder::visit (HIR::BreakExpr &brk) void ExprStmtBuilder::visit (HIR::RangeFromToExpr &range) { - auto from = visit_expr (*range.get_from_expr ()); - auto to = visit_expr (*range.get_to_expr ()); + auto from = visit_expr (range.get_from_expr ()); + auto to = visit_expr (range.get_to_expr ()); return_expr (new InitializerExpr ({from, to}), lookup_type (range), range.get_locus ()); } @@ -447,7 +448,7 @@ ExprStmtBuilder::visit (HIR::RangeFromToExpr &range) void ExprStmtBuilder::visit (HIR::RangeFromExpr &expr) { - auto from = visit_expr (*expr.get_from_expr ()); + auto from = visit_expr (expr.get_from_expr ()); return_expr (new InitializerExpr ({from}), lookup_type (expr), expr.get_locus ()); } @@ -455,7 +456,7 @@ ExprStmtBuilder::visit (HIR::RangeFromExpr &expr) void ExprStmtBuilder::visit (HIR::RangeToExpr &expr) { - auto to = visit_expr (*expr.get_to_expr ()); + auto to = visit_expr (expr.get_to_expr ()); return_expr (new InitializerExpr ({to}), lookup_type (expr), expr.get_locus ()); } @@ -469,8 +470,8 @@ ExprStmtBuilder::visit (HIR::RangeFullExpr &expr) void ExprStmtBuilder::visit (HIR::RangeFromToInclExpr &expr) { - auto from = visit_expr (*expr.get_from_expr ()); - auto to = visit_expr (*expr.get_to_expr ()); + auto from = visit_expr (expr.get_from_expr ()); + auto to = visit_expr (expr.get_to_expr ()); return_expr (new InitializerExpr ({from, to}), lookup_type (expr), expr.get_locus ()); } @@ -478,7 +479,7 @@ ExprStmtBuilder::visit (HIR::RangeFromToInclExpr &expr) void ExprStmtBuilder::visit (HIR::RangeToInclExpr &expr) { - auto to = visit_expr (*expr.get_to_expr ()); + auto to = visit_expr (expr.get_to_expr ()); return_expr (new InitializerExpr ({to}), lookup_type (expr), expr.get_locus ()); } @@ -489,9 +490,9 @@ ExprStmtBuilder::visit (HIR::ReturnExpr &ret) if (ret.has_return_expr ()) { push_assignment (RETURN_VALUE_PLACE, - move_place (visit_expr (*ret.get_expr ()), - ret.get_expr ()->get_locus ()), - ret.get_expr ()->get_locus ()); + move_place (visit_expr (ret.get_expr ()), + ret.get_expr ().get_locus ()), + ret.get_expr ().get_locus ()); } unwind_until (ROOT_SCOPE); push_return (ret.get_locus ()); @@ -509,7 +510,7 @@ ExprStmtBuilder::visit (HIR::LoopExpr &expr) { auto loop = setup_loop (expr); - std::ignore = visit_expr (*expr.get_loop_block ()); + std::ignore = visit_expr (expr.get_loop_block ()); if (!ctx.get_current_bb ().is_terminated ()) push_goto (loop.continue_bb); @@ -521,12 +522,12 @@ ExprStmtBuilder::visit (HIR::WhileLoopExpr &expr) { auto loop = setup_loop (expr); - auto cond_val = visit_expr (*expr.get_predicate_expr ()); + auto cond_val = visit_expr (expr.get_predicate_expr ()); auto body_bb = new_bb (); push_switch (cond_val, expr.get_locus (), {body_bb, loop.break_bb}); ctx.current_bb = body_bb; - std::ignore = visit_expr (*expr.get_loop_block ()); + std::ignore = visit_expr (expr.get_loop_block ()); push_goto (loop.continue_bb); ctx.current_bb = loop.break_bb; @@ -544,15 +545,15 @@ ExprStmtBuilder::visit (HIR::IfExpr &expr) { // If without else cannot return a non-unit value (see [E0317]). - if (expr.get_if_block ()->statements.empty ()) + if (expr.get_if_block ().statements.empty ()) return; - push_switch (visit_expr (*expr.get_if_condition ()), expr.get_locus ()); + push_switch (visit_expr (expr.get_if_condition ()), expr.get_locus ()); BasicBlockId if_block = ctx.current_bb; ctx.current_bb = new_bb (); BasicBlockId then_start_block = ctx.current_bb; - std::ignore = visit_expr (*expr.get_if_block ()); + std::ignore = visit_expr (expr.get_if_block ()); if (!ctx.get_current_bb ().is_terminated ()) push_goto (INVALID_BB); // Resolved later. BasicBlockId then_end_block = ctx.current_bb; @@ -573,8 +574,8 @@ ExprStmtBuilder::visit (HIR::IfExpr &expr) void ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr) { - push_switch (move_place (visit_expr (*expr.get_if_condition ()), - expr.get_if_condition ()->get_locus ()), + push_switch (move_place (visit_expr (expr.get_if_condition ()), + expr.get_if_condition ().get_locus ()), expr.get_locus ()); BasicBlockId if_end_bb = ctx.current_bb; @@ -582,14 +583,14 @@ ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr) ctx.current_bb = new_bb (); BasicBlockId then_start_bb = ctx.current_bb; - std::ignore = visit_expr (*expr.get_if_block (), result); + std::ignore = visit_expr (expr.get_if_block (), result); if (!ctx.get_current_bb ().is_terminated ()) push_goto (INVALID_BB); // Resolved later. BasicBlockId then_end_bb = ctx.current_bb; ctx.current_bb = new_bb (); BasicBlockId else_start_bb = ctx.current_bb; - std::ignore = visit_expr (*expr.get_else_block (), result); + std::ignore = visit_expr (expr.get_else_block (), result); if (!ctx.get_current_bb ().is_terminated ()) push_goto (INVALID_BB); // Resolved later. BasicBlockId else_end_bb = ctx.current_bb; @@ -612,18 +613,6 @@ ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr) } void -ExprStmtBuilder::visit (HIR::IfLetExpr &expr) -{ - rust_sorry_at (expr.get_locus (), "if let expressions are not supported"); -} - -void -ExprStmtBuilder::visit (HIR::IfLetExprConseqElse &expr) -{ - rust_sorry_at (expr.get_locus (), "if let expressions are not supported"); -} - -void ExprStmtBuilder::visit (HIR::MatchExpr &expr) { rust_sorry_at (expr.get_locus (), "match expressions are not supported"); @@ -702,35 +691,35 @@ ExprStmtBuilder::visit (HIR::LetStmt &stmt) tl::optional<TyTy::BaseType *> type_annotation; if (stmt.has_type ()) - type_annotation = lookup_type (*stmt.get_type ()); + type_annotation = lookup_type (stmt.get_type ()); - if (stmt.get_pattern ()->get_pattern_type () == HIR::Pattern::IDENTIFIER) + if (stmt.get_pattern ().get_pattern_type () == HIR::Pattern::IDENTIFIER) { // Only if a pattern is just an identifier, no destructuring is needed. // Hoverer PatternBindingBuilder cannot change existing temporary // (init expr is evaluated before pattern binding) into a // variable, so it would emit extra assignment. - auto var = declare_variable (stmt.get_pattern ()->get_mappings ()); + auto var = declare_variable (stmt.get_pattern ().get_mappings ()); if (stmt.has_type ()) - push_user_type_ascription (var, lookup_type (*stmt.get_type ())); + push_user_type_ascription (var, lookup_type (stmt.get_type ())); if (stmt.has_init_expr ()) - std::ignore = visit_expr (*stmt.get_init_expr (), var); + std::ignore = visit_expr (stmt.get_init_expr (), var); } else { if (stmt.has_init_expr ()) - init = visit_expr (*stmt.get_init_expr ()); + init = visit_expr (stmt.get_init_expr ()); PatternBindingBuilder (ctx, init, type_annotation) - .go (*stmt.get_pattern ()); + .go (stmt.get_pattern ()); } } void ExprStmtBuilder::visit (HIR::ExprStmt &stmt) { - PlaceId result = visit_expr (*stmt.get_expr ()); + PlaceId result = visit_expr (stmt.get_expr ()); // We must read the value for current liveness and we must not store it into // the same place. if (result != INVALID_PLACE) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.h index 574f0f3..daedb68 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.h @@ -101,8 +101,6 @@ protected: // Expr void visit (HIR::IfExprConseqElse &expr) override; void visit (HIR::InlineAsm &expr) override; - void visit (HIR::IfLetExpr &expr) override; - void visit (HIR::IfLetExprConseqElse &expr) override; void visit (HIR::MatchExpr &expr) override; void visit (HIR::AwaitExpr &expr) override; void visit (HIR::AsyncBlockExpr &expr) override; diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h index f499532..3bc622c 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h @@ -63,16 +63,16 @@ public: protected: void visit (HIR::LazyBooleanExpr &expr) override { - auto lhs = visit_expr (*expr.get_lhs ()); - push_switch (move_place (lhs, expr.get_lhs ()->get_locus ()), + auto lhs = visit_expr (expr.get_lhs ()); + push_switch (move_place (lhs, expr.get_lhs ().get_locus ()), expr.get_locus (), {short_circuit_bb}); start_new_consecutive_bb (); - return_place (visit_expr (*expr.get_rhs ()), expr.get_locus ()); + return_place (visit_expr (expr.get_rhs ()), expr.get_locus ()); } void visit (HIR::GroupedExpr &expr) override { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } protected: @@ -193,14 +193,6 @@ public: { return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ()); } - void visit (HIR::IfLetExpr &expr) override - { - return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ()); - } - void visit (HIR::IfLetExprConseqElse &expr) override - { - return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ()); - } void visit (HIR::MatchExpr &expr) override { return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ()); diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc index 723ff73..ee37bb0 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc @@ -50,7 +50,7 @@ PatternBindingBuilder::visit (HIR::ReferencePattern &pattern) return ty->as<TyTy::ReferenceType> ()->get_base (); }); - pattern.get_referenced_pattern ()->accept_vis (*this); + pattern.get_referenced_pattern ().accept_vis (*this); } void @@ -107,7 +107,7 @@ PatternBindingBuilder::visit (HIR::StructPattern &pattern) init = init.map ([&] (PlaceId id) { return ctx.place_db.lookup_or_add_path ( - Place::FIELD, lookup_type (*tuple->get_tuple_pattern ()), id, + Place::FIELD, lookup_type (tuple->get_tuple_pattern ()), id, tuple->get_index ()); }); @@ -120,7 +120,7 @@ PatternBindingBuilder::visit (HIR::StructPattern &pattern) ->get_field_type (); }); - tuple->get_tuple_pattern ()->accept_vis (*this); + tuple->get_tuple_pattern ().accept_vis (*this); break; } case HIR::StructPatternField::IDENT_PAT: { @@ -136,7 +136,7 @@ PatternBindingBuilder::visit (HIR::StructPattern &pattern) field_ty->get_field_type (), saved.init.value (), field_index); - ident_field->get_pattern ()->accept_vis (*this); + ident_field->get_pattern ().accept_vis (*this); break; } case HIR::StructPatternField::IDENT: { @@ -197,17 +197,17 @@ PatternBindingBuilder::visit (HIR::TuplePattern &pattern) SavedState saved (this); size_t index = 0; - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TuplePatternItems::MULTIPLE: { auto &items = static_cast<HIR::TuplePatternItemsMultiple &> ( - *pattern.get_items ()); + pattern.get_items ()); visit_tuple_fields (items.get_patterns (), saved, index); break; } case HIR::TuplePatternItems::RANGED: { auto &items - = static_cast<HIR::TuplePatternItemsRanged &> (*pattern.get_items ()); + = static_cast<HIR::TuplePatternItemsRanged &> (pattern.get_items ()); auto tyty = ctx.place_db[init.value ()].tyty; rust_assert (tyty->get_kind () == TyTy::TUPLE); @@ -242,11 +242,11 @@ PatternBindingBuilder::visit (HIR::TupleStructPattern &pattern) }); size_t index = 0; - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TupleStructItems::RANGED: { auto &items - = static_cast<HIR::TupleStructItemsRange &> (*pattern.get_items ()); + = static_cast<HIR::TupleStructItemsRange &> (pattern.get_items ()); rust_assert (type->get_kind () == TyTy::ADT); auto adt_ty = static_cast<TyTy::ADTType *> (type); @@ -263,7 +263,7 @@ PatternBindingBuilder::visit (HIR::TupleStructPattern &pattern) } case HIR::TupleStructItems::MULTIPLE: { auto &items - = static_cast<HIR::TupleStructItemsNoRange &> (*pattern.get_items ()); + = static_cast<HIR::TupleStructItemsNoRange &> (pattern.get_items ()); visit_tuple_fields (items.get_patterns (), saved, index); break; } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h index 5d4b85a..33ecd23 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h @@ -91,6 +91,7 @@ public: void visit (HIR::QualifiedPathInExpression &expression) override {} void visit (HIR::RangePattern &pattern) override {} }; + } // namespace BIR } // namespace Rust diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h index 53346bf..94fcecd 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h @@ -52,12 +52,12 @@ public: } void visit (HIR::StructExprFieldIdentifierValue &field) override { - auto value = ExprStmtBuilder (ctx).build (*field.get_value ()); + auto value = ExprStmtBuilder (ctx).build (field.get_value ()); handle_named_field (field, value); } void visit (HIR::StructExprFieldIndexValue &field) override { - auto value = ExprStmtBuilder (ctx).build (*field.get_value ()); + auto value = ExprStmtBuilder (ctx).build (field.get_value ()); coercion_site (value, struct_ty->get_field_at_index (field.get_tuple_index ()) ->get_field_type ()); @@ -149,8 +149,6 @@ protected: void visit (HIR::WhileLetLoopExpr &expr) override { rust_unreachable (); } void visit (HIR::IfExpr &expr) override { rust_unreachable (); } void visit (HIR::IfExprConseqElse &expr) override { rust_unreachable (); } - void visit (HIR::IfLetExpr &expr) override { rust_unreachable (); } - void visit (HIR::IfLetExprConseqElse &expr) override { rust_unreachable (); } void visit (HIR::MatchExpr &expr) override { rust_unreachable (); } void visit (HIR::AwaitExpr &expr) override { rust_unreachable (); } void visit (HIR::AsyncBlockExpr &expr) override { rust_unreachable (); } @@ -252,10 +250,6 @@ protected: void visit (HIR::ImplTraitType &type) override { rust_unreachable (); } void visit (HIR::TraitObjectType &type) override { rust_unreachable (); } void visit (HIR::ParenthesisedType &type) override { rust_unreachable (); } - void visit (HIR::ImplTraitTypeOneBound &type) override - { - rust_unreachable (); - } void visit (HIR::TupleType &type) override { rust_unreachable (); } void visit (HIR::NeverType &type) override { rust_unreachable (); } void visit (HIR::RawPointerType &type) override { rust_unreachable (); } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h index 63d3262..e5bdb46 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h @@ -49,7 +49,7 @@ public: for (auto ¶m : function.get_function_params ()) handle_param (param); - handle_body (*function.get_definition ()); + handle_body (function.get_definition ()); auto region_hir_map = map_region_to_hir (function.get_generic_params (), ctx.fn_free_regions); @@ -118,14 +118,14 @@ private: void handle_param (HIR::FunctionParam ¶m) { - auto param_type = lookup_type (*param.get_param_name ()); + auto param_type = lookup_type (param.get_param_name ()); auto &pattern = param.get_param_name (); - if (pattern->get_pattern_type () == HIR::Pattern::IDENTIFIER - && !static_cast<HIR::IdentifierPattern &> (*pattern).get_is_ref ()) + if (pattern.get_pattern_type () == HIR::Pattern::IDENTIFIER + && !static_cast<HIR::IdentifierPattern &> (pattern).get_is_ref ()) { // Avoid useless temporary variable for parameter to look like MIR. - translated = declare_variable (pattern->get_mappings ()); + translated = declare_variable (pattern.get_mappings ()); ctx.arguments.push_back (translated); } else @@ -133,11 +133,9 @@ private: translated = ctx.place_db.add_temporary (param_type); ctx.arguments.push_back (translated); PatternBindingBuilder (ctx, translated, tl::nullopt) - .go (*param.get_param_name ()); + .go (param.get_param_name ()); } - rust_assert (param.get_type () != nullptr); - // Set parameter place to use functions regions, not the fresh ones. ctx.place_db[translated].regions = bind_regions (Resolver::TypeCheckContext::get () @@ -159,7 +157,7 @@ private: body.get_end_locus ()); } auto return_location = body.has_expr () - ? body.get_final_expr ()->get_locus () + ? body.get_final_expr ().get_locus () : body.get_end_locus (); push_return (return_location); } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h index 1332ecf..32a4cd7 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h @@ -815,6 +815,7 @@ protected: // Subset helpers. case TyTy::PLACEHOLDER: case TyTy::INFER: case TyTy::PARAM: + case TyTy::OPAQUE: rust_unreachable (); } rust_unreachable (); diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h b/gcc/rust/checks/errors/borrowck/rust-bir-place.h index a1621b7..67ca90b 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h @@ -485,6 +485,7 @@ private: case TyTy::PROJECTION: // TODO: DUNNO case TyTy::CLOSURE: // TODO: DUNNO case TyTy::DYNAMIC: // TODO: dunno + case TyTy::OPAQUE: return false; } rust_unreachable (); diff --git a/gcc/rust/checks/errors/borrowck/rust-function-collector.h b/gcc/rust/checks/errors/borrowck/rust-function-collector.h index 51109d7..cdb20e8 100644 --- a/gcc/rust/checks/errors/borrowck/rust-function-collector.h +++ b/gcc/rust/checks/errors/borrowck/rust-function-collector.h @@ -56,13 +56,13 @@ protected: void visit (HIR::Function &function) override { functions.push_back (&function); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); } void visit (HIR::ClosureExpr &closure) override { closures.push_back (&closure); - closure.get_expr ()->accept_vis (*this); + closure.get_expr ().accept_vis (*this); } // TODO: recurse for nested closures and functions. @@ -119,8 +119,6 @@ public: void visit (HIR::WhileLetLoopExpr &expr) override {} void visit (HIR::IfExpr &expr) override {} void visit (HIR::IfExprConseqElse &expr) override {} - void visit (HIR::IfLetExpr &expr) override {} - void visit (HIR::IfLetExprConseqElse &expr) override {} void visit (HIR::MatchExpr &expr) override {} void visit (HIR::AwaitExpr &expr) override {} void visit (HIR::AsyncBlockExpr &expr) override {} @@ -182,7 +180,6 @@ public: void visit (HIR::ImplTraitType &type) override {} void visit (HIR::TraitObjectType &type) override {} void visit (HIR::ParenthesisedType &type) override {} - void visit (HIR::ImplTraitTypeOneBound &type) override {} void visit (HIR::TupleType &type) override {} void visit (HIR::NeverType &type) override {} void visit (HIR::RawPointerType &type) override {} diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 01d8ea5..a537c42 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -22,6 +22,7 @@ #include "rust-hir-stmt.h" #include "rust-hir-item.h" #include "rust-attribute-values.h" +#include "rust-immutable-name-resolution-context.h" namespace Rust { namespace Privacy { @@ -93,6 +94,14 @@ static bool is_child_module (Analysis::Mappings &mappings, NodeId parent, NodeId possible_child) { + if (flag_name_resolution_2_0) + { + auto &nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + + return nr_ctx.values.is_module_descendant (parent, possible_child); + } + auto children = mappings.lookup_module_children (parent); if (!children) @@ -118,8 +127,16 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, { NodeId ref_node_id = UNKNOWN_NODEID; + if (flag_name_resolution_2_0) + { + auto &nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + + if (auto id = nr_ctx.lookup (use_id)) + ref_node_id = *id; + } // FIXME: Don't assert here - we might be dealing with a type - if (!resolver.lookup_resolved_name (use_id, &ref_node_id)) + else if (!resolver.lookup_resolved_name (use_id, &ref_node_id)) resolver.lookup_resolved_type (use_id, &ref_node_id); // FIXME: Assert here. For now, we return since this causes issues when @@ -226,10 +243,12 @@ PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings, static_cast<const TyTy::TupleType *> (ty)->get_fields ()) recursive_check (param.get_tyty ()); return; - case TyTy::PLACEHOLDER: - return recursive_check ( - // FIXME: Can we use `resolve` here? Is that what we should do? - static_cast<const TyTy::PlaceholderType *> (ty)->resolve ()); + case TyTy::PLACEHOLDER: { + const auto p = static_cast<const TyTy::PlaceholderType *> (ty); + if (!p->can_resolve ()) + return; + return recursive_check (p->resolve ()); + } case TyTy::PROJECTION: return recursive_check ( static_cast<const TyTy::ProjectionType *> (ty)->get ()); @@ -252,22 +271,21 @@ PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings, // We shouldn't have inference types here, ever case TyTy::INFER: return; + case TyTy::OPAQUE: + return; case TyTy::ERROR: return; } } void -PrivacyReporter::check_type_privacy (const HIR::Type *type) +PrivacyReporter::check_type_privacy (const HIR::Type &type) { - rust_assert (type); - TyTy::BaseType *lookup = nullptr; - rust_assert ( - ty_ctx.lookup_type (type->get_mappings ().get_hirid (), &lookup)); + rust_assert (ty_ctx.lookup_type (type.get_mappings ().get_hirid (), &lookup)); - auto node_mappings = type->get_mappings (); - return check_base_type_privacy (node_mappings, lookup, type->get_locus ()); + auto node_mappings = type.get_mappings (); + return check_base_type_privacy (node_mappings, lookup, type.get_locus ()); } void @@ -317,100 +335,98 @@ PrivacyReporter::visit (HIR::LiteralExpr &) void PrivacyReporter::visit (HIR::BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::DereferenceExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ArrayExpr &expr) { - HIR::ArrayElems &elements = *expr.get_internal_elements (); + HIR::ArrayElems &elements = expr.get_internal_elements (); switch (elements.get_array_expr_type ()) { case HIR::ArrayElems::ArrayExprType::VALUES: { - HIR::ArrayElemsValues &elems - = static_cast<HIR::ArrayElemsValues &> (elements); + auto &elems = static_cast<HIR::ArrayElemsValues &> (elements); for (auto &value : elems.get_values ()) value->accept_vis (*this); } return; case HIR::ArrayElems::ArrayExprType::COPIED: - HIR::ArrayElemsCopied &elems - = static_cast<HIR::ArrayElemsCopied &> (elements); - elems.get_elem_to_copy ()->accept_vis (*this); + auto &elems = static_cast<HIR::ArrayElemsCopied &> (elements); + elems.get_elem_to_copy ().accept_vis (*this); } } void PrivacyReporter::visit (HIR::ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -423,7 +439,7 @@ PrivacyReporter::visit (HIR::TupleExpr &expr) void PrivacyReporter::visit (HIR::TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -439,13 +455,13 @@ PrivacyReporter::visit (HIR::StructExprFieldIdentifier &) void PrivacyReporter::visit (HIR::StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void PrivacyReporter::visit (HIR::StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -458,7 +474,7 @@ PrivacyReporter::visit (HIR::StructExprStructFields &expr) void PrivacyReporter::visit (HIR::CallExpr &expr) { - expr.get_fnexpr ()->accept_vis (*this); + expr.get_fnexpr ().accept_vis (*this); for (auto ¶m : expr.get_arguments ()) param->accept_vis (*this); @@ -467,7 +483,7 @@ PrivacyReporter::visit (HIR::CallExpr &expr) void PrivacyReporter::visit (HIR::MethodCallExpr &expr) { - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto ¶m : expr.get_arguments ()) param->accept_vis (*this); @@ -476,7 +492,7 @@ PrivacyReporter::visit (HIR::MethodCallExpr &expr) void PrivacyReporter::visit (HIR::FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); // FIXME: We should also check if the field is public? } @@ -493,9 +509,8 @@ PrivacyReporter::visit (HIR::BlockExpr &expr) for (auto &stmt : expr.get_statements ()) stmt->accept_vis (*this); - auto &last_expr = expr.get_final_expr (); - if (last_expr) - last_expr->accept_vis (*this); + if (expr.has_final_expr ()) + expr.get_final_expr ().accept_vis (*this); } void @@ -505,28 +520,27 @@ PrivacyReporter::visit (HIR::ContinueExpr &) void PrivacyReporter::visit (HIR::BreakExpr &expr) { - auto &break_expr = expr.get_expr (); - if (break_expr) - break_expr->accept_vis (*this); + if (expr.has_break_expr ()) + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -536,8 +550,8 @@ PrivacyReporter::visit (HIR::RangeFullExpr &) void PrivacyReporter::visit (HIR::RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -549,70 +563,55 @@ PrivacyReporter::visit (HIR::RangeToInclExpr &) void PrivacyReporter::visit (HIR::ReturnExpr &expr) { - if (expr.get_expr ()) - expr.get_expr ()->accept_vis (*this); + if (expr.has_expr ()) + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::UnsafeBlockExpr &expr) { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); -} - -void -PrivacyReporter::visit (HIR::IfLetExpr &) -{ - // TODO: We need to visit the if_let_expr - // TODO: We need to visit the block as well -} - -void -PrivacyReporter::visit (HIR::IfLetExprConseqElse &) -{ - // TODO: We need to visit the if_let_expr - // TODO: We need to visit the if_block as well - // TODO: We need to visit the else_block as well + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); } void @@ -655,9 +654,9 @@ void PrivacyReporter::visit (HIR::Function &function) { for (auto ¶m : function.get_function_params ()) - check_type_privacy (param.get_type ().get ()); + check_type_privacy (param.get_type ()); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); } void @@ -714,14 +713,14 @@ void PrivacyReporter::visit (HIR::ConstantItem &const_item) { // TODO: We need to visit the type - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::StaticItem &static_item) { // TODO: We need to visit the type - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); } void @@ -754,17 +753,17 @@ PrivacyReporter::visit (HIR::EmptyStmt &) void PrivacyReporter::visit (HIR::LetStmt &stmt) { - if (stmt.get_type ()) - check_type_privacy (stmt.get_type ().get ()); + if (stmt.has_type ()) + check_type_privacy (stmt.get_type ()); - if (stmt.get_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + if (stmt.has_init_expr ()) + stmt.get_init_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } } // namespace Privacy diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h index c4f94ab..5111a3e 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h @@ -75,7 +75,7 @@ types * @param type Reference to an explicit type used in a statement, expression * or parameter */ - void check_type_privacy (const HIR::Type *type); + void check_type_privacy (const HIR::Type &type); virtual void visit (HIR::StructExprFieldIdentifier &field); virtual void visit (HIR::StructExprFieldIdentifierValue &field); @@ -121,8 +121,6 @@ types virtual void visit (HIR::WhileLetLoopExpr &expr); virtual void visit (HIR::IfExpr &expr); virtual void visit (HIR::IfExprConseqElse &expr); - virtual void visit (HIR::IfLetExpr &expr); - virtual void visit (HIR::IfLetExprConseqElse &expr); virtual void visit (HIR::MatchExpr &expr); virtual void visit (HIR::AwaitExpr &expr); virtual void visit (HIR::AsyncBlockExpr &expr); diff --git a/gcc/rust/checks/errors/privacy/rust-reachability.cc b/gcc/rust/checks/errors/privacy/rust-reachability.cc index 9e0cb82..1e57674 100644 --- a/gcc/rust/checks/errors/privacy/rust-reachability.cc +++ b/gcc/rust/checks/errors/privacy/rust-reachability.cc @@ -132,7 +132,7 @@ ReachabilityVisitor::visit (HIR::StructStruct &struct_item) { for (auto &field : struct_item.get_fields ()) if (field.get_visibility ().is_public ()) - ctx.update_reachability (field.get_field_type ()->get_mappings (), + ctx.update_reachability (field.get_field_type ().get_mappings (), struct_reach); } diff --git a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc index 464ce86..f0da745 100644 --- a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc +++ b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc @@ -20,6 +20,10 @@ #include "rust-ast.h" #include "rust-hir.h" #include "rust-hir-item.h" +#include "rust-immutable-name-resolution-context.h" + +// for flag_name_resolution_2_0 +#include "options.h" namespace Rust { namespace Privacy { @@ -61,7 +65,22 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction, "cannot use non-module path as privacy restrictor"); NodeId ref_node_id = UNKNOWN_NODEID; - if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) + if (flag_name_resolution_2_0) + { + auto &nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + + if (auto id = nr_ctx.lookup (ast_node_id)) + { + ref_node_id = *id; + } + else + { + invalid_path.emit (); + return false; + } + } + else if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) { invalid_path.emit (); return false; diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index 7938286..0f4bdeb 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -56,7 +56,7 @@ ASTValidation::visit (AST::LoopLabel &label) void ASTValidation::visit (AST::ConstantItem &const_item) { - if (!const_item.has_expr () && context.back () != Context::TRAIT_IMPL) + if (!const_item.has_expr () && ctx.peek () != Kind::TRAIT) { rust_error_at (const_item.get_locus (), "associated constant in %<impl%> without body"); @@ -82,23 +82,19 @@ ASTValidation::visit (AST::Function &function) "functions cannot be both %<const%> and %<async%>"); if (qualifiers.is_const () - && (context.back () == Context::TRAIT_IMPL - || context.back () == Context::TRAIT)) + && (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT)) rust_error_at (function.get_locus (), ErrorCode::E0379, "functions in traits cannot be declared %<const%>"); // may change soon if (qualifiers.is_async () - && (context.back () == Context::TRAIT_IMPL - || context.back () == Context::TRAIT)) + && (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT)) rust_error_at (function.get_locus (), ErrorCode::E0706, "functions in traits cannot be declared %<async%>"); // if not an associated function but has a self parameter - if (context.back () != Context::TRAIT - && context.back () != Context::TRAIT_IMPL - && context.back () != Context::INHERENT_IMPL - && function.has_self_param ()) + if (ctx.peek () != Kind::TRAIT && ctx.peek () != Kind::TRAIT_IMPL + && ctx.peek () != Kind::INHERENT_IMPL && function.has_self_param ()) rust_error_at ( function.get_self_param ().get_locus (), "%<self%> parameter is only allowed in associated functions"); @@ -140,11 +136,11 @@ ASTValidation::visit (AST::Function &function) { if (!function.has_body ()) { - if (context.back () == Context::INHERENT_IMPL - || context.back () == Context::TRAIT_IMPL) + if (ctx.peek () == Kind::INHERENT_IMPL + || ctx.peek () == Kind::TRAIT_IMPL) rust_error_at (function.get_locus (), "associated function in %<impl%> without body"); - else if (context.back () != Context::TRAIT) + else if (ctx.peek () != Kind::TRAIT) rust_error_at (function.get_locus (), "free function without a body"); } diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index 84c09dd..4904322 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -161,72 +161,72 @@ ConstChecker::visit (LiteralExpr &) void ConstChecker::visit (BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (DereferenceExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void @@ -239,11 +239,11 @@ ConstChecker::visit (ArrayElemsValues &elems) void ConstChecker::visit (ArrayElemsCopied &elems) { - elems.get_elem_to_copy ()->accept_vis (*this); + elems.get_elem_to_copy ().accept_vis (*this); const_context.enter (elems.get_mappings ().get_hirid ()); - elems.get_num_copies_expr ()->accept_vis (*this); + elems.get_num_copies_expr ().accept_vis (*this); const_context.exit (); } @@ -251,14 +251,14 @@ ConstChecker::visit (ArrayElemsCopied &elems) void ConstChecker::visit (ArrayExpr &expr) { - expr.get_internal_elements ()->accept_vis (*this); + expr.get_internal_elements ().accept_vis (*this); } void ConstChecker::visit (ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -271,7 +271,7 @@ ConstChecker::visit (TupleExpr &expr) void ConstChecker::visit (TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -285,13 +285,13 @@ ConstChecker::visit (StructExprFieldIdentifier &) void ConstChecker::visit (StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void ConstChecker::visit (StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -352,10 +352,10 @@ ConstChecker::check_function_call (HirId fn_id, location_t locus) void ConstChecker::visit (CallExpr &expr) { - if (!expr.get_fnexpr ()) + if (!expr.has_fnexpr ()) return; - NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid (); + NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; if (flag_name_resolution_2_0) @@ -388,7 +388,7 @@ ConstChecker::visit (CallExpr &expr) void ConstChecker::visit (MethodCallExpr &expr) { - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto &arg : expr.get_arguments ()) arg->accept_vis (*this); @@ -397,13 +397,13 @@ ConstChecker::visit (MethodCallExpr &expr) void ConstChecker::visit (FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); } void ConstChecker::visit (ClosureExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -413,7 +413,7 @@ ConstChecker::visit (BlockExpr &expr) stmt->accept_vis (*this); if (expr.has_expr ()) - expr.get_final_expr ()->accept_vis (*this); + expr.get_final_expr ().accept_vis (*this); } void @@ -424,26 +424,26 @@ void ConstChecker::visit (BreakExpr &expr) { if (expr.has_break_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void ConstChecker::visit (RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void ConstChecker::visit (RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -453,8 +453,8 @@ ConstChecker::visit (RangeFullExpr &) void ConstChecker::visit (RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -467,73 +467,57 @@ void ConstChecker::visit (ReturnExpr &expr) { if (expr.has_return_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (UnsafeBlockExpr &expr) { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void ConstChecker::visit (LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void ConstChecker::visit (WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void ConstChecker::visit (WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void ConstChecker::visit (IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void ConstChecker::visit (IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); -} - -void -ConstChecker::visit (IfLetExpr &expr) -{ - expr.get_scrutinee_expr ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); -} - -void -ConstChecker::visit (IfLetExprConseqElse &expr) -{ - expr.get_scrutinee_expr ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - - // TODO: Visit else expression + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void ConstChecker::visit (MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); for (auto &match_arm : expr.get_match_cases ()) - match_arm.get_expr ()->accept_vis (*this); + match_arm.get_expr ().accept_vis (*this); } void @@ -606,9 +590,9 @@ ConstChecker::visit (Function &function) ConstGenericCtx::Function); for (auto ¶m : function.get_function_params ()) - param.get_type ()->accept_vis (*this); + param.get_type ().accept_vis (*this); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); if (const_fn) const_context.exit (); @@ -652,7 +636,7 @@ ConstChecker::visit (EnumItemDiscriminant &item) { const_context.enter (item.get_mappings ().get_hirid ()); - item.get_discriminant_expression ()->accept_vis (*this); + item.get_discriminant_expression ().accept_vis (*this); const_context.exit (); } @@ -676,7 +660,7 @@ ConstChecker::visit (ConstantItem &const_item) { const_context.enter (const_item.get_mappings ().get_hirid ()); - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); const_context.exit (); } @@ -686,7 +670,7 @@ ConstChecker::visit (StaticItem &static_item) { const_context.enter (static_item.get_mappings ().get_hirid ()); - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); const_context.exit (); } @@ -694,15 +678,15 @@ ConstChecker::visit (StaticItem &static_item) void ConstChecker::visit (TraitItemFunc &item) { - if (item.has_block_defined ()) - item.get_block_expr ()->accept_vis (*this); + if (item.has_definition ()) + item.get_block_expr ().accept_vis (*this); } void ConstChecker::visit (TraitItemConst &item) { if (item.has_expr ()) - item.get_expr ()->accept_vis (*this); + item.get_expr ().accept_vis (*this); } void @@ -837,13 +821,13 @@ void ConstChecker::visit (LetStmt &stmt) { if (stmt.has_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } void ConstChecker::visit (ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } void @@ -863,10 +847,6 @@ ConstChecker::visit (ParenthesisedType &) {} void -ConstChecker::visit (ImplTraitTypeOneBound &) -{} - -void ConstChecker::visit (TupleType &) {} @@ -891,7 +871,7 @@ ConstChecker::visit (ArrayType &type) { const_context.enter (type.get_mappings ().get_hirid ()); - type.get_size_expr ()->accept_vis (*this); + type.get_size_expr ().accept_vis (*this); const_context.exit (); } diff --git a/gcc/rust/checks/errors/rust-const-checker.h b/gcc/rust/checks/errors/rust-const-checker.h index 8890761..00f57988 100644 --- a/gcc/rust/checks/errors/rust-const-checker.h +++ b/gcc/rust/checks/errors/rust-const-checker.h @@ -128,8 +128,6 @@ private: virtual void visit (WhileLetLoopExpr &expr) override; virtual void visit (IfExpr &expr) override; virtual void visit (IfExprConseqElse &expr) override; - virtual void visit (IfLetExpr &expr) override; - virtual void visit (IfLetExprConseqElse &expr) override; virtual void visit (MatchExpr &expr) override; virtual void visit (AwaitExpr &expr) override; virtual void visit (AsyncBlockExpr &expr) override; @@ -193,7 +191,6 @@ private: virtual void visit (ImplTraitType &type) override; virtual void visit (TraitObjectType &type) override; virtual void visit (ParenthesisedType &type) override; - virtual void visit (ImplTraitTypeOneBound &type) override; virtual void visit (TupleType &type) override; virtual void visit (NeverType &type) override; virtual void visit (RawPointerType &type) override; diff --git a/gcc/rust/checks/errors/rust-feature.h b/gcc/rust/checks/errors/rust-feature.h index e2082c5..9edae6d 100644 --- a/gcc/rust/checks/errors/rust-feature.h +++ b/gcc/rust/checks/errors/rust-feature.h @@ -19,7 +19,7 @@ #ifndef RUST_FEATURE_H #define RUST_FEATURE_H -#include "rust-session-manager.h" +#include "rust-edition.h" #include "optional.h" namespace Rust { @@ -66,7 +66,7 @@ private: Feature (Name name, State state, const char *name_str, const char *rustc_since, tl::optional<unsigned> issue_number = tl::nullopt, - const tl::optional<CompileOptions::Edition> &edition = tl::nullopt, + const tl::optional<Edition> &edition = tl::nullopt, const char *description = "") : m_state (state), m_name (name), m_name_str (name_str), m_rustc_since (rustc_since), m_issue (issue_number), edition (edition), @@ -78,7 +78,7 @@ private: std::string m_name_str; std::string m_rustc_since; tl::optional<unsigned> m_issue; - tl::optional<CompileOptions::Edition> edition; + tl::optional<Edition> edition; std::string m_description; // TODO: Switch to optional? static const std::map<std::string, Name> name_hash_map; diff --git a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc index f46f429..257f4cd 100644 --- a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc +++ b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc @@ -92,72 +92,72 @@ PatternChecker::visit (LiteralExpr &) void PatternChecker::visit (BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (DereferenceExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void @@ -170,20 +170,20 @@ PatternChecker::visit (ArrayElemsValues &elems) void PatternChecker::visit (ArrayElemsCopied &elems) { - elems.get_elem_to_copy ()->accept_vis (*this); + elems.get_elem_to_copy ().accept_vis (*this); } void PatternChecker::visit (ArrayExpr &expr) { - expr.get_internal_elements ()->accept_vis (*this); + expr.get_internal_elements ().accept_vis (*this); } void PatternChecker::visit (ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -196,7 +196,7 @@ PatternChecker::visit (TupleExpr &expr) void PatternChecker::visit (TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -210,13 +210,13 @@ PatternChecker::visit (StructExprFieldIdentifier &) void PatternChecker::visit (StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void PatternChecker::visit (StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -233,10 +233,10 @@ PatternChecker::visit (StructExprStructBase &) void PatternChecker::visit (CallExpr &expr) { - if (!expr.get_fnexpr ()) + if (!expr.has_fnexpr ()) return; - NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid (); + NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; if (flag_name_resolution_2_0) { @@ -266,7 +266,7 @@ PatternChecker::visit (CallExpr &expr) void PatternChecker::visit (MethodCallExpr &expr) { - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto &arg : expr.get_arguments ()) arg->accept_vis (*this); @@ -275,13 +275,13 @@ PatternChecker::visit (MethodCallExpr &expr) void PatternChecker::visit (FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); } void PatternChecker::visit (ClosureExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -291,7 +291,7 @@ PatternChecker::visit (BlockExpr &expr) stmt->accept_vis (*this); if (expr.has_expr ()) - expr.get_final_expr ()->accept_vis (*this); + expr.get_final_expr ().accept_vis (*this); } void @@ -302,26 +302,26 @@ void PatternChecker::visit (BreakExpr &expr) { if (expr.has_break_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PatternChecker::visit (RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void PatternChecker::visit (RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -331,92 +331,76 @@ PatternChecker::visit (RangeFullExpr &) void PatternChecker::visit (RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PatternChecker::visit (RangeToInclExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PatternChecker::visit (ReturnExpr &expr) { if (expr.has_return_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (UnsafeBlockExpr &expr) { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void PatternChecker::visit (LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PatternChecker::visit (WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PatternChecker::visit (WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PatternChecker::visit (IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void PatternChecker::visit (IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); -} - -void -PatternChecker::visit (IfLetExpr &expr) -{ - expr.get_scrutinee_expr ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); -} - -void -PatternChecker::visit (IfLetExprConseqElse &expr) -{ - expr.get_scrutinee_expr ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - - expr.get_else_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void PatternChecker::visit (MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); for (auto &match_arm : expr.get_match_cases ()) - match_arm.get_expr ()->accept_vis (*this); + match_arm.get_expr ().accept_vis (*this); // match expressions are only an entrypoint TyTy::BaseType *scrutinee_ty; bool ok = tyctx.lookup_type ( - expr.get_scrutinee_expr ()->get_mappings ().get_hirid (), &scrutinee_ty); + expr.get_scrutinee_expr ().get_mappings ().get_hirid (), &scrutinee_ty); rust_assert (ok); check_match_usefulness (&tyctx, scrutinee_ty, expr); @@ -484,7 +468,7 @@ PatternChecker::visit (UseDeclaration &) void PatternChecker::visit (Function &function) { - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); } void @@ -526,27 +510,27 @@ PatternChecker::visit (Union &) void PatternChecker::visit (ConstantItem &const_item) { - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); } void PatternChecker::visit (StaticItem &static_item) { - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); } void PatternChecker::visit (TraitItemFunc &item) { - if (item.has_block_defined ()) - item.get_block_expr ()->accept_vis (*this); + if (item.has_definition ()) + item.get_block_expr ().accept_vis (*this); } void PatternChecker::visit (TraitItemConst &item) { if (item.has_expr ()) - item.get_expr ()->accept_vis (*this); + item.get_expr ().accept_vis (*this); } void @@ -675,13 +659,13 @@ void PatternChecker::visit (LetStmt &stmt) { if (stmt.has_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } void PatternChecker::visit (ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } void @@ -701,10 +685,6 @@ PatternChecker::visit (ParenthesisedType &) {} void -PatternChecker::visit (ImplTraitTypeOneBound &) -{} - -void PatternChecker::visit (TupleType &) {} @@ -1177,33 +1157,34 @@ WitnessMatrix::extend (const WitnessMatrix &other) // forward declarations static DeconstructedPat -lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, +lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern &pattern, TyTy::BaseType *scrutinee_ty); static DeconstructedPat lower_tuple_pattern (Resolver::TypeCheckContext *ctx, - HIR::TupleStructPattern *pattern, + HIR::TupleStructPattern &pattern, TyTy::VariantDef *variant, Constructor &ctor) { int arity = variant->get_fields ().size (); - HIR::TupleStructItems *elems = pattern->get_items ().get (); + HIR::TupleStructItems &elems = pattern.get_items (); std::vector<DeconstructedPat> fields; - switch (elems->get_item_type ()) + switch (elems.get_item_type ()) { case HIR::TupleStructItems::ItemType::MULTIPLE: { - HIR::TupleStructItemsNoRange *multiple - = static_cast<HIR::TupleStructItemsNoRange *> (elems); + HIR::TupleStructItemsNoRange &multiple + = static_cast<HIR::TupleStructItemsNoRange &> (elems); rust_assert (variant->get_fields ().size () - == multiple->get_patterns ().size ()); - for (size_t i = 0; i < multiple->get_patterns ().size (); i++) + == multiple.get_patterns ().size ()); + + for (size_t i = 0; i < multiple.get_patterns ().size (); i++) { fields.push_back ( - lower_pattern (ctx, multiple->get_patterns ().at (i).get (), + lower_pattern (ctx, *multiple.get_patterns ().at (i), variant->get_fields ().at (i)->get_field_type ())); } - return DeconstructedPat (ctor, arity, fields, pattern->get_locus ()); + return DeconstructedPat (ctor, arity, fields, pattern.get_locus ()); } break; case HIR::TupleStructItems::ItemType::RANGED: { @@ -1219,7 +1200,7 @@ lower_tuple_pattern (Resolver::TypeCheckContext *ctx, static DeconstructedPat lower_struct_pattern (Resolver::TypeCheckContext *ctx, - HIR::StructPattern *pattern, TyTy::VariantDef *variant, + HIR::StructPattern &pattern, TyTy::VariantDef *variant, Constructor ctor) { int arity = variant->get_fields ().size (); @@ -1227,7 +1208,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, // Initialize all field patterns to wildcard. std::vector<DeconstructedPat> fields = std::vector<DeconstructedPat> (arity, DeconstructedPat::make_wildcard ( - pattern->get_locus ())); + pattern.get_locus ())); std::map<std::string, int> field_map; for (int i = 0; i < arity; i++) @@ -1237,7 +1218,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, } // Fill in the fields with the present patterns. - HIR::StructPatternElements elems = pattern->get_struct_pattern_elems (); + HIR::StructPatternElements elems = pattern.get_struct_pattern_elems (); for (auto &elem : elems.get_struct_pattern_fields ()) { switch (elem->get_item_type ()) @@ -1248,7 +1229,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, int field_idx = field_map.at (ident->get_identifier ().as_string ()); fields.at (field_idx) - = DeconstructedPat::make_wildcard (pattern->get_locus ()); + = DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::StructPatternField::ItemType::IDENT_PAT: { @@ -1257,7 +1238,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, int field_idx = field_map.at (ident_pat->get_identifier ().as_string ()); fields.at (field_idx) = lower_pattern ( - ctx, ident_pat->get_pattern ().get (), + ctx, ident_pat->get_pattern (), variant->get_fields ().at (field_idx)->get_field_type ()); } break; @@ -1272,19 +1253,19 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, } } - return DeconstructedPat{ctor, arity, fields, pattern->get_locus ()}; + return DeconstructedPat{ctor, arity, fields, pattern.get_locus ()}; }; static DeconstructedPat -lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, +lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern &pattern, TyTy::BaseType *scrutinee_ty) { - HIR::Pattern::PatternType pat_type = pattern->get_pattern_type (); + HIR::Pattern::PatternType pat_type = pattern.get_pattern_type (); switch (pat_type) { case HIR::Pattern::PatternType::WILDCARD: case HIR::Pattern::PatternType::IDENTIFIER: { - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::PATH: { @@ -1292,12 +1273,12 @@ lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, // structs // https://doc.rust-lang.org/reference/patterns.html#path-patterns // unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::REFERENCE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::STRUCT: @@ -1305,15 +1286,15 @@ lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, HirId path_id = UNKNOWN_HIRID; if (pat_type == HIR::Pattern::PatternType::STRUCT) { - HIR::StructPattern *struct_pattern - = static_cast<HIR::StructPattern *> (pattern); - path_id = struct_pattern->get_path ().get_mappings ().get_hirid (); + HIR::StructPattern &struct_pattern + = static_cast<HIR::StructPattern &> (pattern); + path_id = struct_pattern.get_path ().get_mappings ().get_hirid (); } else { - HIR::TupleStructPattern *tuple_pattern - = static_cast<HIR::TupleStructPattern *> (pattern); - path_id = tuple_pattern->get_path ().get_mappings ().get_hirid (); + HIR::TupleStructPattern &tuple_pattern + = static_cast<HIR::TupleStructPattern &> (pattern); + path_id = tuple_pattern.get_path ().get_mappings ().get_hirid (); } rust_assert (scrutinee_ty->get_kind () == TyTy::TypeKind::ADT); @@ -1346,46 +1327,46 @@ lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, if (pat_type == HIR::Pattern::PatternType::STRUCT) { - HIR::StructPattern *struct_pattern - = static_cast<HIR::StructPattern *> (pattern); + HIR::StructPattern &struct_pattern + = static_cast<HIR::StructPattern &> (pattern); return lower_struct_pattern (ctx, struct_pattern, variant, ctor); } else { - HIR::TupleStructPattern *tuple_pattern - = static_cast<HIR::TupleStructPattern *> (pattern); + HIR::TupleStructPattern &tuple_pattern + = static_cast<HIR::TupleStructPattern &> (pattern); return lower_tuple_pattern (ctx, tuple_pattern, variant, ctor); } } break; case HIR::Pattern::PatternType::TUPLE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::SLICE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::ALT: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::LITERAL: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::RANGE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::GROUPED: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; default: { @@ -1401,8 +1382,7 @@ lower_arm (Resolver::TypeCheckContext *ctx, HIR::MatchCase &arm, rust_assert (arm.get_arm ().get_patterns ().size () > 0); DeconstructedPat pat - = lower_pattern (ctx, arm.get_arm ().get_patterns ().at (0).get (), - scrutinee_ty); + = lower_pattern (ctx, *arm.get_arm ().get_patterns ().at (0), scrutinee_ty); return MatchArm (pat, arm.get_arm ().has_match_arm_guard ()); } @@ -1511,8 +1491,9 @@ emit_exhaustiveness_error (Resolver::TypeCheckContext *ctx, HIR::MatchExpr &expr, WitnessMatrix &witness) { TyTy::BaseType *scrutinee_ty; - bool ok = ctx->lookup_type ( - expr.get_scrutinee_expr ()->get_mappings ().get_hirid (), &scrutinee_ty); + bool ok + = ctx->lookup_type (expr.get_scrutinee_expr ().get_mappings ().get_hirid (), + &scrutinee_ty); rust_assert (ok); if (!witness.empty ()) @@ -1530,7 +1511,7 @@ emit_exhaustiveness_error (Resolver::TypeCheckContext *ctx, if (i != witness.get_stacks ().size () - 1) buf << " and "; } - rust_error_at (expr.get_scrutinee_expr ()->get_locus (), + rust_error_at (expr.get_scrutinee_expr ().get_locus (), "non-exhaustive patterns: %s not covered", buf.str ().c_str ()); } @@ -1545,6 +1526,9 @@ void check_match_usefulness (Resolver::TypeCheckContext *ctx, TyTy::BaseType *scrutinee_ty, HIR::MatchExpr &expr) { + if (!expr.has_match_arms ()) + return; + // Lower the arms to a more convenient representation. std::vector<MatrixRow> rows; for (auto &arm : expr.get_match_cases ()) diff --git a/gcc/rust/checks/errors/rust-hir-pattern-analysis.h b/gcc/rust/checks/errors/rust-hir-pattern-analysis.h index 1af02ba..2171340 100644 --- a/gcc/rust/checks/errors/rust-hir-pattern-analysis.h +++ b/gcc/rust/checks/errors/rust-hir-pattern-analysis.h @@ -102,8 +102,6 @@ private: virtual void visit (WhileLetLoopExpr &expr) override; virtual void visit (IfExpr &expr) override; virtual void visit (IfExprConseqElse &expr) override; - virtual void visit (IfLetExpr &expr) override; - virtual void visit (IfLetExprConseqElse &expr) override; virtual void visit (HIR::MatchExpr &expr) override; virtual void visit (AwaitExpr &expr) override; virtual void visit (AsyncBlockExpr &expr) override; @@ -166,7 +164,6 @@ private: virtual void visit (ImplTraitType &type) override; virtual void visit (TraitObjectType &type) override; virtual void visit (ParenthesisedType &type) override; - virtual void visit (ImplTraitTypeOneBound &type) override; virtual void visit (TupleType &type) override; virtual void visit (NeverType &type) override; virtual void visit (RawPointerType &type) override; diff --git a/gcc/rust/checks/errors/rust-readonly-check.cc b/gcc/rust/checks/errors/rust-readonly-check.cc index b899898..c128933 100644 --- a/gcc/rust/checks/errors/rust-readonly-check.cc +++ b/gcc/rust/checks/errors/rust-readonly-check.cc @@ -19,10 +19,13 @@ #include "rust-readonly-check.h" #include "rust-tree.h" #include "rust-gcc.h" +#include "print-tree.h" namespace Rust { namespace Analysis { +static std::map<tree, int> assignment_map = {}; + // ported over from c-family/c-warn.cc void readonly_error (location_t loc, tree arg, enum lvalue_use use) @@ -106,37 +109,68 @@ readonly_error (location_t loc, tree arg, enum lvalue_use use) } static void -check_decl (tree *t) +emit_error (tree *t, tree lhs, enum lvalue_use use) { - if (TREE_CODE (*t) == MODIFY_EXPR) + readonly_error (EXPR_LOCATION (*t), lhs, use); + TREE_OPERAND (*t, 0) = error_mark_node; +} + +static void +check_modify_expr (tree *t) +{ + tree lhs = TREE_OPERAND (*t, 0); + if (TREE_CODE (lhs) == ARRAY_REF || TREE_CODE (lhs) == COMPONENT_REF) + lhs = TREE_OPERAND (lhs, 0); + + tree lhs_type = TREE_TYPE (lhs); + if (TYPE_READONLY (lhs_type) || TREE_READONLY (lhs) || TREE_CONSTANT (lhs)) { - tree lhs = TREE_OPERAND (*t, 0); - if (TREE_READONLY (lhs) || TREE_CONSTANT (lhs)) + if (TREE_CODE (lhs) != VAR_DECL) + emit_error (t, lhs, lv_assign); + else if (!DECL_ARTIFICIAL (lhs)) { - readonly_error (EXPR_LOCATION (*t), lhs, lv_assign); - TREE_OPERAND (*t, 0) = error_mark_node; + if (DECL_INITIAL (lhs) != NULL) + emit_error (t, lhs, lv_assign); + else + { + if (assignment_map.find (lhs) == assignment_map.end ()) + { + assignment_map.insert ({lhs, 0}); + } + assignment_map[lhs]++; + + if (assignment_map[lhs] > 1) + emit_error (t, lhs, lv_assign); + } } } } -static tree -readonly_walk_fn (tree *t, int *, void *) +static void +check_decl (tree *t) { switch (TREE_CODE (*t)) { case MODIFY_EXPR: - check_decl (t); + check_modify_expr (t); break; default: break; } +} + +static tree +readonly_walk_fn (tree *t, int *, void *) +{ + check_decl (t); return NULL_TREE; } void ReadonlyCheck::Lint (Compile::Context &ctx) { + assignment_map.clear (); for (auto &fndecl : ctx.get_func_decls ()) { for (tree p = DECL_ARGUMENTS (fndecl); p != NULL_TREE; p = DECL_CHAIN (p)) @@ -148,12 +182,14 @@ ReadonlyCheck::Lint (Compile::Context &ctx) &readonly_walk_fn, &ctx); } + assignment_map.clear (); for (auto &var : ctx.get_var_decls ()) { tree decl = var->get_decl (); check_decl (&decl); } + assignment_map.clear (); for (auto &const_decl : ctx.get_const_decls ()) { check_decl (&const_decl); diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 4c8db3a..8aa59ee 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -23,6 +23,10 @@ #include "rust-hir-item.h" #include "rust-attribute-values.h" #include "rust-system.h" +#include "rust-immutable-name-resolution-context.h" + +// for flag_name_resolution_2_0 +#include "options.h" namespace Rust { namespace HIR { @@ -216,8 +220,23 @@ UnsafeChecker::visit (PathInExpression &path) NodeId ast_node_id = path.get_mappings ().get_nodeid (); NodeId ref_node_id; - if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) - return; + if (flag_name_resolution_2_0) + { + auto &nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + + auto resolved = nr_ctx.lookup (ast_node_id); + + if (!resolved.has_value ()) + return; + + ref_node_id = resolved.value (); + } + else + { + if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) + return; + } if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id)) { @@ -260,14 +279,14 @@ UnsafeChecker::visit (LiteralExpr &) void UnsafeChecker::visit (BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (DereferenceExpr &expr) { TyTy::BaseType *to_deref_type; - auto to_deref = expr.get_expr ()->get_mappings ().get_hirid (); + auto to_deref = expr.get_expr ().get_mappings ().get_hirid (); rust_assert (context.lookup_type (to_deref, &to_deref_type)); @@ -280,60 +299,60 @@ UnsafeChecker::visit (DereferenceExpr &expr) void UnsafeChecker::visit (ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void @@ -346,20 +365,20 @@ UnsafeChecker::visit (ArrayElemsValues &elems) void UnsafeChecker::visit (ArrayElemsCopied &elems) { - elems.get_elem_to_copy ()->accept_vis (*this); + elems.get_elem_to_copy ().accept_vis (*this); } void UnsafeChecker::visit (ArrayExpr &expr) { - expr.get_internal_elements ()->accept_vis (*this); + expr.get_internal_elements ().accept_vis (*this); } void UnsafeChecker::visit (ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -372,7 +391,7 @@ UnsafeChecker::visit (TupleExpr &expr) void UnsafeChecker::visit (TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -386,13 +405,13 @@ UnsafeChecker::visit (StructExprFieldIdentifier &) void UnsafeChecker::visit (StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void UnsafeChecker::visit (StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -409,17 +428,32 @@ UnsafeChecker::visit (StructExprStructBase &) void UnsafeChecker::visit (CallExpr &expr) { - if (!expr.get_fnexpr ()) + if (!expr.has_fnexpr ()) return; - NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid (); + NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; // There are no unsafe types, and functions are defined in the name resolver. // If we can't find the name, then we're dealing with a type and should return // early. - if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) - return; + if (flag_name_resolution_2_0) + { + auto &nr_ctx + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); + + auto resolved = nr_ctx.lookup (ast_node_id); + + if (!resolved.has_value ()) + return; + + ref_node_id = resolved.value (); + } + else + { + if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) + return; + } if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id)) { @@ -448,14 +482,14 @@ UnsafeChecker::visit (MethodCallExpr &expr) context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (), &method_type); - auto fn = *static_cast<TyTy::FnType *> (method_type); + auto &fn = static_cast<TyTy::FnType &> (*method_type); auto method = mappings.lookup_hir_implitem (fn.get_ref ()); if (!unsafe_context.is_in_context () && method) check_unsafe_call (static_cast<Function *> (method->first), expr.get_locus (), "method"); - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto &arg : expr.get_arguments ()) arg->accept_vis (*this); @@ -464,14 +498,14 @@ UnsafeChecker::visit (MethodCallExpr &expr) void UnsafeChecker::visit (FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); if (unsafe_context.is_in_context ()) return; TyTy::BaseType *receiver_ty; auto ok = context.lookup_type ( - expr.get_receiver_expr ()->get_mappings ().get_hirid (), &receiver_ty); + expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver_ty); rust_assert (ok); if (receiver_ty->get_kind () == TyTy::TypeKind::ADT) @@ -487,7 +521,7 @@ UnsafeChecker::visit (FieldAccessExpr &expr) void UnsafeChecker::visit (ClosureExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -497,7 +531,7 @@ UnsafeChecker::visit (BlockExpr &expr) stmt->accept_vis (*this); if (expr.has_expr ()) - expr.get_final_expr ()->accept_vis (*this); + expr.get_final_expr ().accept_vis (*this); } void @@ -508,26 +542,26 @@ void UnsafeChecker::visit (BreakExpr &expr) { if (expr.has_break_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -537,21 +571,21 @@ UnsafeChecker::visit (RangeFullExpr &) void UnsafeChecker::visit (RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeToInclExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void UnsafeChecker::visit (ReturnExpr &expr) { if (expr.has_return_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -559,7 +593,7 @@ UnsafeChecker::visit (UnsafeBlockExpr &expr) { unsafe_context.enter (expr.get_mappings ().get_hirid ()); - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); unsafe_context.exit (); } @@ -567,61 +601,45 @@ UnsafeChecker::visit (UnsafeBlockExpr &expr) void UnsafeChecker::visit (LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void UnsafeChecker::visit (WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void UnsafeChecker::visit (WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void UnsafeChecker::visit (IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void UnsafeChecker::visit (IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); -} - -void -UnsafeChecker::visit (IfLetExpr &expr) -{ - expr.get_scrutinee_expr ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); -} - -void -UnsafeChecker::visit (IfLetExprConseqElse &expr) -{ - expr.get_scrutinee_expr ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - - // TODO: Visit else expression + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void UnsafeChecker::visit (MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); for (auto &match_arm : expr.get_match_cases ()) - match_arm.get_expr ()->accept_vis (*this); + match_arm.get_expr ().accept_vis (*this); } void @@ -698,7 +716,7 @@ UnsafeChecker::visit (Function &function) if (is_unsafe_fn) unsafe_context.enter (function.get_mappings ().get_hirid ()); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); if (is_unsafe_fn) unsafe_context.exit (); @@ -746,27 +764,27 @@ UnsafeChecker::visit (Union &) void UnsafeChecker::visit (ConstantItem &const_item) { - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (StaticItem &static_item) { - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (TraitItemFunc &item) { - if (item.has_block_defined ()) - item.get_block_expr ()->accept_vis (*this); + if (item.has_definition ()) + item.get_block_expr ().accept_vis (*this); } void UnsafeChecker::visit (TraitItemConst &item) { if (item.has_expr ()) - item.get_expr ()->accept_vis (*this); + item.get_expr ().accept_vis (*this); } void @@ -913,13 +931,13 @@ void UnsafeChecker::visit (LetStmt &stmt) { if (stmt.has_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } void UnsafeChecker::visit (ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } void @@ -939,10 +957,6 @@ UnsafeChecker::visit (ParenthesisedType &) {} void -UnsafeChecker::visit (ImplTraitTypeOneBound &) -{} - -void UnsafeChecker::visit (TupleType &) {} diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h b/gcc/rust/checks/errors/rust-unsafe-checker.h index 1fa1fe0..63098fe 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.h +++ b/gcc/rust/checks/errors/rust-unsafe-checker.h @@ -110,12 +110,10 @@ private: virtual void visit (WhileLetLoopExpr &expr) override; virtual void visit (IfExpr &expr) override; virtual void visit (IfExprConseqElse &expr) override; - virtual void visit (IfLetExpr &expr) override; - virtual void visit (IfLetExprConseqElse &expr) override; virtual void visit (MatchExpr &expr) override; virtual void visit (AwaitExpr &expr) override; virtual void visit (AsyncBlockExpr &expr) override; - virtual void visit (InlineAsm &expr); + virtual void visit (InlineAsm &expr) override; virtual void visit (TypeParam ¶m) override; virtual void visit (ConstGenericParam ¶m) override; virtual void visit (LifetimeWhereClauseItem &item) override; @@ -174,7 +172,6 @@ private: virtual void visit (ImplTraitType &type) override; virtual void visit (TraitObjectType &type) override; virtual void visit (ParenthesisedType &type) override; - virtual void visit (ImplTraitTypeOneBound &type) override; virtual void visit (TupleType &type) override; virtual void visit (NeverType &type) override; virtual void visit (RawPointerType &type) override; |