Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#222] CallStack attach #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

[#222] CallStack attach #261

wants to merge 1 commit into from

Conversation

DK318
Copy link
Member

@DK318 DK318 commented Apr 16, 2022

Description

Problem

Functions from Universum.Unsafe don't carry CallStack.
Sometimes it is hard to debug when you don't have stack trace.

Solution

Attached CallStack to all functions from Universum.Unsafe
by copying them from base and replacing errorWithoutStackTrace with
error function.

Related issues(s)

✓ Checklist for your Pull Request

Ideally a PR has all of the checkmarks set.

If something in this list is irrelevant to your PR, you should still set this
checkmark indicating that you are sure it is dealt with (be that by irrelevance).

  • I made sure my PR addresses a single concern, or multiple concerns which
    are inextricably linked. Otherwise I should open multiple PR's.
  • If your PR fixes/relates to an open issue then the description should
    reference this issue. See also auto linking on
    github
    .

Related changes (conditional)

  • Tests

    • If I added new functionality, I added tests covering it.
    • If I fixed a bug, I added a regression test to prevent the bug from
      silently reappearing again.
  • Documentation

    I checked whether I should update the docs and did so if necessary:

  • Record your changes

    • I added an entry to the changelog if my changes are visible to the users
      and
    • provided a migration guide for breaking changes if possible

Stylistic guide (mandatory)

  • My commit history is clean (only contains changes relating to my
    issue/pull request and no reverted-my-earlier-commit changes) and commit
    messages start with identifiers of related issues in square brackets.

    Example: [#42] Short commit description

    If necessary both of these can be achieved even after the commits have been
    made/pushed using rebase and squash.

Problem: functions from `Universum.Unsafe` don't carry `CallStack`.
Sometimes it is hard to debug when you don't have stack trace.

Solution: attached `CallStack` to all functions from `Universum.Unsafe`
by copying them from `base` and replacing `errorWithoutStackTrace` with
`error` function.
Copy link
Member

@gromakovsky gromakovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we define

head :: HasCallStack => [a] -> a
head = Prelude.head

?
(probably with INLINE)

AFAIU the call stack will contain a reference to Universum.Unsafe, which is what we want, and at the same time we won't have to copy-paste code from base.

import Data.Maybe (fromJust)

import Universum.Base (Int)
( head
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that all files use 7-space indentation for exports, let's keep it consistent throughout the repo and not change it.

@DK318
Copy link
Member Author

DK318 commented Apr 21, 2022

I've looked through Prelude sources and figured out that all these unsafe functions use errorWithoutStackTrace

@gromakovsky
Copy link
Member

Ah, ok, I get it. Well, looks sad, it's something that wasn't considered in the issue description, I mean the fact of copy-paste wasn't mentioned in "Cons".

@dcastro
Copy link
Member

dcastro commented Apr 28, 2022

I mean the fact of copy-paste wasn't mentioned in "Cons".

That's a good point :/ Would you still say this is worth pursuing, despite the cons? Or do you think we shouldn't do this?

@gromakovsky
Copy link
Member

I personally don't remember much suffering from lack of CallStack in Unsafe functions. And I quite heavily don't like copy-paste. So I would refraing from making this change, but it's highly opinionated and if there are enough people who need call stacks I won't reject it. cc @Martoon-00 maybe

@Martoon-00
Copy link
Member

Martoon-00 commented Apr 28, 2022

Fair, I didn't mention that this issue will require copy-paste.

And the fact that we have to copy stuff like INLINE pragmas is pretty annoying (fortunately not the rewrite rules, but who knows, maybe they will appear in the future 🙈).


Though nevertheless, I think this change is important. Not having a callstack seems innocent until the moment when it actually fires, and then it would be extremely hard to debug given a large project. Imagine, I used Unsafe.head 10 times in different parts of the project - that would be sad to investigate.

Coming to think about it, I would consider 3 alternatives:

  1. Add callstacks
  2. Don't have callstacks, leave everything as-is
  3. Remove the Unsafe module altogether

And 2nd option seems the worst here, because having an unsafe function without a callstack, if I understand it correctly, can be treated as an anti-pattern. We better enforce the user to use a safe function + explicit fromMaybe (error ...) (3rd option) than allow using Unsafe.head in its current form.

at n xs = xs !! n

errorEmptyList :: HasCallStack => String -> a
Copy link
Member

@Martoon-00 Martoon-00 Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, written this way we will see at least two entries in each callstack - the one containing the target function (like head) and the one with errorEmptyList. I think there is no reason to include the latter, and probably freezeCallStack will help here.

Though this is subject to discussion too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider attaching CallStack to functions from Unsafe
4 participants