Git Product home page Git Product logo

Comments (3)

Luv-Ray avatar Luv-Ray commented on August 24, 2024

My thought after some debug:
There are some ways to trigger panic in compiler/rustc_pattern_analysis, because we trust that the type analysed here are all correct but they may not.

Consider two programs below:

// program A
enum NestedEnum {
    First,
    Second,
}

enum Enum {
    Variant(*const ()),
    // Variant(*const &'a ()),
}

fn foo(x: Enum) {
    match x {
        Enum::Variant(NestedEnum::Second) => {}
    }
}
// program B
enum NestedEnum {
    First,
    Second,
}

enum Enum {
    // Variant(*const ()),
    Variant(*const &'a ()),
}

fn foo(x: Enum) {
    match x {
        Enum::Variant(NestedEnum::Second) => {}
    }
}

program B will panic at compile while program A not. The difference occurs here:

pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
let typeck_results = tcx.typeck(def_id);
let (thir, expr) = tcx.thir_body(def_id)?;
let thir = thir.borrow();

program A will early return at tcx.thir_body(def_id)?, but program B will not and cause the panic.
I have checked the typeck_results of two programs, and there is a key different between them: in program A typeck_results.tainted_by_errors is Some(ErrorGuaranteed(())), and in program B that is None.

A:

TypeckResults { hir_owner: DefId(0:12 ~ tmp[d0fa]::foo), type_dependent_defs: UnordMap { inner: {} }, field_indices: UnordMap { inner: {} }, nested_fields: UnordMap { inner: {} }, node_types: UnordMap { inner: {16: (), 13: (), 10: NestedEnum, 4: Enum, 1: Enum, 14: (), 11: Enum, 2: Enum, 15: (), 3: ()} }, node_args: UnordMap { inner: {} }, user_provided_types: UnordMap { inner: {} }, user_provided_sigs: UnordMap { inner: {} }, adjustments: UnordMap { inner: {} }, pat_binding_modes: UnordMap { inner: {2: BindingMode(No, Not)} }, rust_2024_migration_desugared_pats: UnordSet { inner: {} }, pat_adjustments: UnordMap { inner: {} }, skipped_ref_pats: UnordSet { inner: {} }, closure_kind_origins: UnordMap { inner: {} }, liberated_fn_sigs: UnordMap { inner: {0: fn(Enum)} }, fru_field_types: UnordMap { inner: {} }, coercion_casts: UnordSet { inner: {} }, used_trait_imports: UnordSet { inner: {} }, tainted_by_errors: Some(ErrorGuaranteed(())), concrete_opaque_types: {}, closure_min_captures: UnordMap { inner: {} }, closure_fake_reads: UnordMap { inner: {} }, rvalue_scopes: RvalueScopes { map: UnordMap { inner: {} } }, coroutine_stalled_predicates: {}, treat_byte_string_as_slice: UnordSet { inner: {} }, closure_size_eval: UnordMap { inner: {} }, offset_of_data: UnordMap { inner: {} }, inline_consts: {} }

B:

TypeckResults { hir_owner: DefId(0:12 ~ tmp2[62c9]::foo), type_dependent_defs: UnordMap { inner: {} }, field_indices: UnordMap { inner: {} }, nested_fields: UnordMap { inner: {} }, node_types: UnordMap { inner: {16: (), 13: (), 10: NestedEnum, 4: Enum, 1: Enum, 14: (), 11: Enum, 2: Enum, 15: (), 3: ()} }, node_args: UnordMap { inner: {} }, user_provided_types: UnordMap { inner: {} }, user_provided_sigs: UnordMap { inner: {} }, adjustments: UnordMap { inner: {} }, pat_binding_modes: UnordMap { inner: {2: BindingMode(No, Not)} }, rust_2024_migration_desugared_pats: UnordSet { inner: {} }, pat_adjustments: UnordMap { inner: {} }, skipped_ref_pats: UnordSet { inner: {} }, closure_kind_origins: UnordMap { inner: {} }, liberated_fn_sigs: UnordMap { inner: {0: fn(Enum)} }, fru_field_types: UnordMap { inner: {} }, coercion_casts: UnordSet { inner: {} }, used_trait_imports: UnordSet { inner: {} }, tainted_by_errors: None, concrete_opaque_types: {}, closure_min_captures: UnordMap { inner: {} }, closure_fake_reads: UnordMap { inner: {} }, rvalue_scopes: RvalueScopes { map: UnordMap { inner: {} } }, coroutine_stalled_predicates: {}, treat_byte_string_as_slice: UnordSet { inner: {} }, closure_size_eval: UnordMap { inner: {} }, offset_of_data: UnordMap { inner: {} }, inline_consts: {} }

In TyCtxt::thir_body's comment, it says: If typeck for that body failed, returns an empty Thir., but seems that it return ErrorGuaranteed rather than empty Thir if typeck failed. Is the description wrong here?

from rust.

Luv-Ray avatar Luv-Ray commented on August 24, 2024

Because this hack, I think.

// HACK: We specifically don't want the (opaque) error from tainting our
// inference context. That'll prevent us from doing opaque type inference
// later on in borrowck, which affects diagnostic spans pretty negatively.
if let Some(e) = fcx.tainted_by_errors() {
wbcx.typeck_results.tainted_by_errors = Some(e);
}

from rust.

Luv-Ray avatar Luv-Ray commented on August 24, 2024

program A set tainted_by_errors to Some(ErrorGuaranteed(())) here, but program B not.

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.